Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

How to do the loop

1 view (last 30 days)
Aswin Sandirakumaran
Aswin Sandirakumaran on 5 May 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comment
Dennis
Dennis on 5 May 2018
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.

Answers (0)

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!