Filter löschen
Filter löschen

Sort vector using loops

2 Ansichten (letzte 30 Tage)
Amjad Green
Amjad Green am 8 Apr. 2018
Verschoben: Voss am 22 Aug. 2022
Im asked to write a code using loops(if,for while..)that will sort an input vector
  3 Kommentare

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Swaroopa
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:
Hope it helps.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by