How to sort any size matrix elements from largest to smallest using loop?

I am trying to write a function that rearranges the elements of any size matrix in a descending order row after row with the (1,1) element the largest and the (m,n) element the smallest without using the built in functions max, sort, or min. My code only doesn't work when the number of columns of the input matrix is 3. Can someone please tell what am I doing wrong?
function B=matrixsort(A)
[m,n]=size(A);
i=1;
j=1;
a=1;
b=2;
while i~=m || j~=n
if (A(a,b)>A(i,j) && b<n)
r=A(i,j);
A(i,j)=A(a,b);
A(a,b)=r;
b=b+1;
elseif (A(a,b)>A(i,j) && b==n && a<m)
r=A(i,j);
A(i,j)=A(a,b);
A(a,b)=r;
a=a+1;
b=1;
elseif (A(a,b)>A(i,j) && b==n && a==m && j<n)
r=A(i,j);
A(i,j)=A(a,b);
A(a,b)=r;
j=j+1;
if (j==n)
a=i+1;
b=1;
else
a=i;
b=j+1;
end
elseif (A(a,b)>A(i,j) && b==n && a==m && j==n)
r=A(i,j);
A(i,j)=A(a,b);
A(a,b)=r;
i=i+1;
j=1;
a=i;
b=b+1;
elseif (A(a,b)<=A(i,j) && b<n)
b=b+1;
elseif (A(a,b)<=A(i,j) && b==n && a<m)
a=a+1;
b=1;
elseif (A(a,b)<=A(i,j) && b==n && a==m && j<n)
j=j+1;
if (j==n)
a=i+1;
b=1;
else
a=i;
b=j+1;
end
else
i=i+1;
j=1;
a=i;
b=j+1;
end
end
B=A;

댓글 수: 1

Closing your question when you've been given many useful answers is very disrespectful of the people who have given up valuable time to help you.
Rather, you should be accepting and or voting for the answers.

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

답변 (3개)

One simple way is to use Bubble Sorting algorithm:
v=[4 7 2 9 6 1;8 2 9 4 0 5] %demo data
for i=1:size(v,1)
for k=1:size(v,2)
for j=1:size(v,2)-1
if v(i,j)<v(i,j+1)
temp=v(i,j+1);
v(i,j+1)=v(i,j);
v(i,j)=temp;
end
end
end
end
v %sorted demo data

댓글 수: 2

thank you. but why does my code not work when the number of columns is 3?
No idea. Your code is really hard to keep track of. You need to learn how to write more proper codes.

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

Guillaume
Guillaume 2018년 4월 9일
편집: Guillaume 2018년 4월 9일
I have not looked in enough details at your code to know why it doesn't work for n = 3. I suspect that it does not work for a lot more conditions. There is certainly an error in your while condition which should be:
while i~=m && j~=n
There are probably more. Learn to use the debugger. Step through your program line by line and see how the variables change as you execute each line. You'll quickly find out where it goes wrong.
I would strongly recommend you separate the test for the element swap from the test for the index increments. Something like
while ...
if A(a, b) > A(i, j)
%do swap
end
%now do index increment, again seperating the test for column from the test for row
if b < n
%increase b
else
b = 1;
if a < m
%increase a
else
%reached end of array increase i,j
%...
end
end
end
Finally, I'll point out that your sorting algorithm is extremely inefficient, the complexity is O(n^2) which is extremely bad (worst case is when all the elements are already sorted descending). There are much better algorithms.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

Ami
2018년 4월 9일

댓글:

2018년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by