How would one reverse the order of an array using a while loop?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Justin Keach
am 30 Okt. 2016
Kommentiert: Walter Roberson
am 31 Okt. 2016
For this homework question I am asked to write a program using the while loop that will display a given row or column vector in reverse.
0 Kommentare
Akzeptierte Antwort
Asad (Mehrzad) Khoddam
am 30 Okt. 2016
if v is the vector:
n=length(v);
while n>0
disp(v(n));
n=n-1;
end
2 Kommentare
Weitere Antworten (1)
Walter Roberson
am 30 Okt. 2016
Supposed the length of your vector is L. Then you want to copy position j to position L-j+1 in the new vector. For example, length 6, position 1 gets written to position 6-1+1 = 6; position 2 gets written to position 6-2+1 = 5, position 3 to position 6-3+1 = 4, and so on.
2 Kommentare
Walter Roberson
am 31 Okt. 2016
In your code above,
k=length(v);
and then
L = k;
and then entry k of input corresponds to entry L-k+1 of output.
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!