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

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

1 件のコメント

Cesar Franco
Cesar Franco 2018 年 3 月 22 日
What is the [C I] you've placed on the 4th line?

サインインしてコメントする。

 採用された回答

the cyclist
the cyclist 2014 年 8 月 8 日

0 投票

In place of
x2(k)=min(x)
use
x2(k)=C

1 件のコメント

Adam Palmer
Adam Palmer 2014 年 8 月 8 日
Thanks the cyclist! Im so glad it worked, because I found it quite eloquent

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File 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