필터 지우기
필터 지우기

cumulative sum that restarts whenever it reaches certain level

조회 수: 7 (최근 30일)
Dam
Dam 2014년 10월 14일
댓글: Amanda S 2020년 5월 24일
Good evening I have a matrix A of 250*3 what i try to do is the following: the first time that the cumsum "of raws" is > 6, i replace the corresponding value in matrix A with zero and restart the cumsum form the value after the zero. then we repeat this once more: the first time the new cumsum >6 we replace it with zero and restart the cum sum we repeat this untill we arrive to the last raw of the matrix
Thank u in advance

채택된 답변

Sean de Wolski
Sean de Wolski 2014년 10월 14일
rng default
x = randi(3,[20 1]);
thresh = 6;
while 1
xc = cumsum(x);
k = find(xc > thresh,1,'first');
if isempty(k)
% done
break
end
x(k) = -xc(k-1);
end
When you say set that row equal to zero, the cumsum on the next row will still be greater than 6. (something greater than 6 + 0 is still greater than 6). Thus you need to set the value to the negative of the previous value in order to reset the cumulative sum. If you need zeros at the end, remove the negatives.
% If you need to zero out those points
x(x<0) = 0
  댓글 수: 1
Amanda S
Amanda S 2020년 5월 24일
Where in the code is the command that says to replace the number that comes next in the cumsum that is greater than 6, with a zero? Im doing a similiar thing but i also want to include the number that in this case, is replaced with a zero, but I don't know how. I believe that the change need to be done in the second row from the bottom but I cant seem to get it to work my way.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by