Sort vector using loops
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Im asked to write a code using loops(if,for while..)that will sort an input vector
3 Kommentare
Antworten (1)
Swaroopa
am 22 Aug. 2022
Verschoben: Voss
am 22 Aug. 2022
You can sort a vector using for loops in the following way in MATLAB.
function res=vectorsort(ar, n)
for i=1:n
for j=1:n-i
if ar(j) > ar(j+1)
t=ar(j);
ar(j)=ar(j+1);
ar(j+1)=ar(j);
end
end
end
res=ar;
end
Refer to the below documentation for more information:
https://www.mathworks.com/help/matlab/ref/function.html and https://www.mathworks.com/help/matlab/ref/for.html
Hope it helps.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices 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!