Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

How to do the loop

1 Ansicht (letzte 30 Tage)
Aswin Sandirakumaran
Aswin Sandirakumaran am 5 Mai 2018
Geschlossen: MATLAB Answer Bot am 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 Kommentar
Dennis
Dennis am 5 Mai 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.

Antworten (0)

Diese Frage ist geschlossen.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by