gpt4 book ai didi

matlab - 同一时间单位的值的总和

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:14 24 4
gpt4 key购买 nike

我有以下向量:

A=[1 0 1 0 0 1 0 1 0 0];
B=[1 2 3 4 5 6 7 8 9 10];

在这种情况下,A 表示一个时间向量,其中 1 表示一个时间单位的开始。现在我想将 B 中的所有值加起来,这些值对应于具有相同长度的 3 个步骤的时间单位。因此,在此示例中,这意味着 B 的第 3、4 和 5 个值以及第 8、9 和 10 个值应该相加,因为它们在长度为 3 的时间单位中。

B_result=[12 27];

我知道 cumsum() 是用于此的命令,但我不知道如何说应该只对取决于 A 的时间索引的这些特定值求和。

你能帮帮我吗?

非常感谢

最佳答案

您可以使用 cumsum旁边accumarrayhist :

csa = cumsum(A); %// from begining og unit to unit indices
n = hist(csa, 1:max(csa)); %// count num of steps in each unit
B_result = accumarray( csa', B' ); %// accumulate B into different time units
B_result(n~=3) = []; %// discard all time units that do not have 3 steps

关于matlab - 同一时间单位的值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24996389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com