Info

此问题已关闭。 请重新打开它进行编辑或回答。

How to do the loop

1 次查看(过去 30 天)
Aswin Sandirakumaran
关闭: MATLAB Answer Bot 2021-8-20
I have a vector A = [0,0,0] & B = [4,2,1]
i = 1:length(B) -->> I am not sure about this part
while (A(i) <= B(i))
A(i) = A(i) + 1;
end
I need to perform this while loop according to B values. To give you a clear picture; first while loop should consider B(1) which is 4. Therefore while loop should be carried out 4 times. here the output will of A will change to A = [4,0,0]. Once done, B(2)--> which is 2. So while loop should be done 2 times and o/p now will be A = [4,2,0] and then B(3) ---> which is 1. So the Final output of A will be [4,2,1] instead of [0,0,0].
  1 个评论
Dennis
Dennis 2018-5-5
to make your code work you just need to add 'for'
for it = 1:length(B)
while (A(it) <= B(it))
A(it) = A(it) + 1;
end
end
However it might be possible to avoid using multiple loops, depending on what you want to do. Right now A=B would do the trick but i assume thats not what you actually want.

回答(0 个)

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by