How to convert from a for loop to a while loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Yuqing Zhu
am 13 Dez. 2016
Bearbeitet: James Tursa
am 13 Dez. 2016
I need to modify the set of codes below to a while loop
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158962/image.png)
3 Kommentare
James Tursa
am 13 Dez. 2016
Bearbeitet: James Tursa
am 13 Dez. 2016
Why do you need this converted? Is this homework? Why not get rid of the loop entirely and vectorize this operation?
Akzeptierte Antwort
James Tursa
am 13 Dez. 2016
Bearbeitet: James Tursa
am 13 Dez. 2016
Your loop doesn't work because the "continue" statement skips over the Index=Index+1 statement. You need to rewrite things without skipping over that statement ... e.g., maybe try to rewrite your if-then-else stuff so that you do not use a "continue" statement. (Hint: What happens to your logic if you simply comment out that "continue" statement?)
2 Kommentare
James Tursa
am 13 Dez. 2016
Bearbeitet: James Tursa
am 13 Dez. 2016
See my hint about how to get your while loop version working. Or see Dave Barry's one-liner.
Weitere Antworten (1)
David Barry
am 13 Dez. 2016
This does not need to be any kind of loop. You should learn about logical indexing in MATLAB. This can be done with one line of code.
Vector(Vector >=0) = Vector(Vector >=0) * 2;
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!