Vector with various IFs, adding up and counting down
1 回表示 (過去 30 日間)
古いコメントを表示
I need to translate an Excel formula to whatsapp.
I have one vector X: [ 5 2 -4 -6 -2 1 4 2 -3 -6 -1 8 9 ]
Now, vector y needs to accumulate if vector x is negative and the opposite when positive until back to zero: [ 0 0 -4 -10 -12 -11 -7 -5 -8 -14 -15 -7 0 ]
I have been looking through Matlab functions but don't come far. Is there any easy way to do this on Matlab?
Many thanks in advance!
0 件のコメント
回答 (1 件)
Paolo
2018 年 6 月 19 日
X = [ 5 2 -4 -6 -2 1 4 2 -3 -6 -1 8 9 ];
indx = find(X<0);
Y = [zeros(1,indx(1)-1) cumsum(X(indx(1):end))];
Y(Y>0) = 0;
Y =
Columns 1 through 11
0 0 -4 -10 -12 -11 -7 -5 -8 -14 -15
Columns 12 through 13
-7 0
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!