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

30 ビュー (過去 30 日間)
Ami
Ami 2018 年 4 月 9 日
コメント済み: Guillaume 2018 年 4 月 30 日
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 件のコメント
Guillaume
Guillaume 2018 年 4 月 30 日
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 件)

Birdman
Birdman 2018 年 4 月 9 日
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 件のコメント
Ami
Ami 2018 年 4 月 9 日
thank you. but why does my code not work when the number of columns is 3?
Birdman
Birdman 2018 年 4 月 9 日
No idea. Your code is really hard to keep track of. You need to learn how to write more proper codes.

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


KSSV
KSSV 2018 年 4 月 9 日

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.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by