Effacer les filtres
Effacer les filtres

How to sort a vector from smallest to largest without sort command

2 vues (au cours des 30 derniers jours)
Adam Palmer
Adam Palmer le 8 Août 2014
Commenté : Cesar Franco le 22 Mar 2018
Hey matlab community. I have another question for you all. I need to sort a vector from smallest to largest without using the sort command. I'm open to any suggestions, so you don't have to stick to my code. Here is what I tried to do: I found the location of the min of the vector x, and tried to remove it/store it into my 'empty' vector x2. So on each iteration a new minimum is found and thusly sorts from smallest to largest.
x=[-3.5 5 -6.2 11.1 0 7 -9.5 2 15 -1 3 2.5];
x2=[1:length(x)]
for k=1:length(x)
[C I]=min(x);
x(I)=[];
x2(k)=min(x);
end
Your wisdom is appreciated

Réponse acceptée

the cyclist
the cyclist le 8 Août 2014
In place of
x2(k)=min(x)
use
x2(k)=C
  1 commentaire
Adam Palmer
Adam Palmer le 8 Août 2014
Thanks the cyclist! Im so glad it worked, because I found it quite eloquent

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by