필터 지우기
필터 지우기

Sort a matrix without using the sort function

조회 수: 8 (최근 30일)
Roland
Roland 2011년 5월 5일
To study, I am trying to sort a matrix using for loops. I used a template I had for finding a max.
[row,column]=size(a);
sortmx=zeros(size(a));
for n=1:column
mx=a(1,n);
for p=2:row
if a(p,n)>mx
mx=a(p,n);
else
mx=a(p-1,n);
end
end
sortmx(n,p)=mx
end

답변 (1개)

Paulo Silva
Paulo Silva 2011년 5월 5일
[row,column]=size(a);
for nc=1:column
for nr=1:row
[C,I]=max(a(nr:row,nc));
b=a(nr,nc);
a(nr,nc)=C;
a(I(1)+nr-1,nc)=b;
end
end
  댓글 수: 2
Roland
Roland 2011년 5월 5일
I am failing at getting anything correct, how would order each number in an individual column rather than find one value?
Matt Fig
Matt Fig 2011년 5월 5일
Did you search MATLAB Answers first? I found at least two identical questions from past posters....
This must be a popular homework question indeed!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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