hello I am trying to sort a matrix in ascending order by column. I know I can just use the sort function but this is a homework assignment and it requires me to do so(being honest). I feel like I am very close to finishing this pseudo code but not quite there. the problem I am having is that I only end up with the last column of the matrix ( sorted ) , what i do know is that each time the "for" statement executes the last column that was sorted gets over written next one. if someone can help me approach this (or solve with explanation) that would be great, but i do prefer a guidance over an exact answer. thanks in advance. (below is a picture of the final result) when i do not suppress "s" i can clearly see that all my columns are being sorted, just stumped on fixing it...

a = randi([0,100],10,5);
[r,c] = size(a);
temp = 0;
for x = 1:c
    s=a(:,x);
    for jj=1:r
        for ii = 1:jj
            if(s(ii)>s(jj))
                temp=s(ii);
                s(ii)=s(jj);
                s(jj)=temp;
            end
        end
    end
end

3 件のコメント

OCDER
OCDER 2018 年 6 月 26 日
編集済み: OCDER 2018 年 6 月 26 日
At some point, you need to save your sorted s column vector back to your matrix a or a new matrix of equal size as a.
Note also that your sort is sorting each column of a independently, as in it's different than sortrows.
joel perez
joel perez 2018 年 6 月 26 日
編集済み: joel perez 2018 年 6 月 26 日
Sorry i dont have my laptop with me right now but would it be as simple as placing...
a(:,x)=s
Right before my first end?that's what i would attempt it I could code it at the moment
per isakson
per isakson 2018 年 6 月 26 日
"would it be as simple as placing" YES!

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2018 年 6 月 26 日

コメント済み:

2018 年 6 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by