Is there a "go to" command in MATLAB
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Martin Matisoff
am 14 Apr. 2017
Bearbeitet: John D'Errico
am 14 Apr. 2017
Is there a 'go to' command in MATLAB? I want to increment a variable and then return to the point where the routine begins. Right now, the variable reverts to the value that I started with.
a = 1
x = 3
for i = a:n
y = factorial(x);
disp(y)
end
a = a + 1
When I run the code as written above, the variable 'a' reverts to the original value.
1 Kommentar
Akzeptierte Antwort
Star Strider
am 14 Apr. 2017
Your ‘a’ value is outside the loop you posted, so it never gets incremented except after the loop. (This assumes there is not an outer loop we don’t know about, likely because you’ve not defined ‘n’ in your posted code.) You’re not incrementing ‘x’ at all, so ‘y’ will never change.
It will be easier to help you if we know what you want to do.
0 Kommentare
Weitere Antworten (1)
John D'Errico
am 14 Apr. 2017
Bearbeitet: John D'Errico
am 14 Apr. 2017
I have no idea what you really wanted that code to do.
But essentially always, when someone pleads for a goto statement in MATLAB, it just means they have not learned how to use existing features in MATLAB. Here a while loop might be appropriate, allowing you to return to a point above. Or, you might put some of that code in a function, then just call the function repeatedly in a for (or while) loop.
There is much you can do, with absolutely no need for a goto here.
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!