how to sum by 2 indeces?

1 回表示 (過去 30 日間)
Tiina
Tiina 2016 年 1 月 28 日
編集済み: Andrei Bobrov 2016 年 1 月 28 日
A=[1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3]';
b=[2 3 2]';
c=[9 8 7 6 4 3 4 4 4 5 3 5 1 2 3 4 5 6 7]';
out=[17 13 7 12 13 3 7 11 7]';
A is a unique index, b is a process signal that indicates summation blocks (in other words non-overlapping). The relationship between A and b is that for every unique A there is a unique B. c is data. out(1,1) = 17 because in A where index is 1, b is the first nonoverlapping sum for the first 2 observations 9+8 and so on. If there is one index then accumarray sums c according to unique in A. But this has 2 indeces A and b and an odd observation 7 at the end of the row because it remains without being summed up. How is out produced in this case?

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 1 月 28 日
編集済み: Andrei Bobrov 2016 年 1 月 28 日
i0 = [true;diff(A)~=0];
ii = [find(i0);numel(A)+1];
for jj = 1:numel(b)
i0(ii(jj):b(jj):ii(jj+1)) = 1;
end
out = accumarray(cumsum(i0),c);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by