Adding values in an array within limits
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have a vector A=[1 2 3 4 5 4]'
I would like to get from vector A, a vector B where its elements are the same with A, until the cumulative sum becomes 9. When the limit of 9 is reached the rest of the elements will be zero.
So the vector B will be : B=[1 2 3 3 0 0]'
I don't know if I explained it well
thanks in advance
Nikolas
0 个评论
采纳的回答
更多回答(1 个)
Stephen23
2017-3-29
>> A = [1;2;3;3;5;4];
>> B = A.*(cumsum(A)<=9)
B =
1
2
3
3
0
0
2 个评论
Stephen23
2017-3-29
编辑:Stephen23
2017-3-29
@Nikolas Spiliopoulos: you can use whatever vector you want.
I just used the vector that fitted your desired output, which you gave as "B=[1 2 3 3 0 0]'". Did I guess wrong? If you could please explain the reason for why the fourth elements of A and B should be different, then I can show you how to achieve what you want.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!