Sort vector using loops

2 次查看(过去 30 天)
Amjad Green
Amjad Green 2018-4-8
移动Voss 2022-8-22
Im asked to write a code using loops(if,for while..)that will sort an input vector

回答(1 个)

Swaroopa
Swaroopa 2022-8-22
移动:Voss 2022-8-22
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.

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by