Quick way to sum array elements based on flag in another array?

4 ビュー (過去 30 日間)
Ted
Ted 2015 年 7 月 14 日
コメント済み: Ted 2015 年 7 月 15 日
Say I have 2 arrays: one with "clean" data, and one that tells me which data gets lost, as indicated by zeros. If an element is lost, it should be added to the next observed element. Anybody got a quick one-or-two-liner to come up with the summed array?
Example:
clean = [1 2 3 4 5 6]
lost = [1 0 3 0 0 6]
desired = [1 5 15]
  1 件のコメント
Sean de Wolski
Sean de Wolski 2015 年 7 月 14 日
Fun question, well asked with example inputs and expected outputs +1

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 7 月 14 日
編集済み: Stephen23 2015 年 7 月 14 日
Try this:
tmp = (1:numel(lost))-[0,cumsum(lost(1:end-1)==0)];
desired = accumarray(tmp.',clean).'
Tested:
>> clean = [1 2 3 4 5 6];
>> lost = [1 0 3 0 0 6];
>> tmp = (1:numel(lost))-[0,cumsum(lost(1:end-1)==0)];
>> desired = accumarray(tmp.',clean).'
desired =
1 5 15
>> clean = [40 10 50 30 40 10 50 30 40 10 50 30];
>> lost = [40 0 50 30 0 10 0 0 40 10 50 30];
>> tmp = (1:numel(lost))-[0,cumsum(lost(1:end-1)==0)];
>> desired = accumarray(tmp.',clean).'
desired =
40 60 30 50 120 10 50 30

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2015 年 7 月 14 日
編集済み: Andrei Bobrov 2015 年 7 月 14 日
desired = flip(accumarray(cumsum(lost(end:-1:1)~=0)',clean(end:-1:1)),1)';
or
desired = accumarray(cumsum(xor([0,diff(lost > 0)],lost))',clean)';
  2 件のコメント
Ted
Ted 2015 年 7 月 14 日
Thanks for the help. Try:
clean = [40 10 50 30 40 10 50 30 40 10 50 30];
lost = [40 0 50 30 0 10 0 0 40 10 50 30];
It should give:
desired = [40 60 30 50 120 10 50 30];
Your solution does not.
Andrei Bobrov
Andrei Bobrov 2015 年 7 月 14 日
corrected

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2015 年 7 月 14 日
accumarray(interp1(find(lost),1:nnz(lost),1:numel(lost),'next')',clean)
  4 件のコメント
Andrei Bobrov
Andrei Bobrov 2015 年 7 月 14 日
+1
Ted
Ted 2015 年 7 月 15 日
Sorry Sean, I must have fat-fingered the test!

サインインしてコメントする。

カテゴリ

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