Sort vector using loops

2 visualizaciones (últimos 30 días)
Amjad Green
Amjad Green el 8 de Abr. de 2018
Movida: Voss el 22 de Ag. de 2022
Im asked to write a code using loops(if,for while..)that will sort an input vector
  3 comentarios

Iniciar sesión para comentar.

Respuestas (1)

Swaroopa
Swaroopa el 22 de Ag. de 2022
Movida: Voss el 22 de Ag. de 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.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by