Cumsum function + range of values after cumsum.

A 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 ..... 0 0 1 0 0 0 0 1 0 0 1 0 1 ....
cumsum 0 0 0 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 ..... 159 159 160 0 0 0 0 1 1 1 2 2 3 ....
Delete 0 0 0 1 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 ..... 0 0 160 0 0 0 0 1 0 0 2 0 3 ....
Ex) When the vaule 'Delete' = 160, I'd like to reset counter 160 -> 1.
for i=1:n;
cumsum=cumsum(A); => some codes should be added to reset...;;
if A(i,1)==A(i+1,1);
0=Delete(i+1,1);
end
end
Thank you.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 11 月 17 日
編集済み: Andrei Bobrov 2015 年 11 月 17 日

1 投票

cmsum0 = cumsum(A);
cmsum = rem(cmsum0-1,160)+1;
delte = A;
t = A > 0;
delte(t) = cmsum(t);

3 件のコメント

the cyclist
the cyclist 2015 年 11 月 17 日
This method retains multiple instances of 160 in cmsum beyond the first one, which it seems he did not want.
Andrei Bobrov
Andrei Bobrov 2015 年 11 月 17 日
other variant:
cmsum = cumsum(A);
t = cmsum > 160;
cmsum(t) = cmsum(t) - 160;
delte = cmsum.*A;
the cyclist
the cyclist 2015 年 11 月 17 日
Two comments on this variant:
  • I don't think it addresses the reoccurrences of exactly 160 that I mentioned in an earlier comment.
  • You might need to do the "subtract 160" step multiple times, if the cumsum could results in values greater than 320, 480, 640, etc. This is easily done via a while loop.

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

その他の回答 (1 件)

the cyclist
the cyclist 2015 年 11 月 17 日

0 投票

I think this does what you want. I changed the name of your cumsum variable to c. It's bad practice (and confusing) to name a variable that same as a MATLAB function.
A = [0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 156 0 0 0 0 1 0 0 1 0 1];
n = numel(A);
c = cumsum(A);
reset = 160;
for i = 2:n
if (c(i) > reset+1) || (c(i-1)==reset && c(i)==reset)
c(i:end) = c(i:end) - reset;
end
end
Delete = c;
Delete([false diff(c)==0]) = 0;

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2015 年 11 月 17 日

コメント済み:

2015 年 11 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by