pseudo code (sort)

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 Comments

OCDER
OCDER on 26 Jun 2018
Edited: OCDER on 26 Jun 2018
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 on 26 Jun 2018
Edited: joel perez on 26 Jun 2018
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 on 26 Jun 2018
"would it be as simple as placing" YES!

Sign in to comment.

Answers (0)

Categories

Asked:

on 26 Jun 2018

Commented:

on 26 Jun 2018

Community Treasure Hunt

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

Start Hunting!