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 Exchange에서 Shifting 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