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.
채택된 답변
추가 답변 (1개)
the cyclist
2015년 11월 17일
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 Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!