Sorting a matrix from smallest to biggest left to right
    6 次查看(过去 30 天)
  
       显示 更早的评论
    
I made this code here for a matrix that was only 1 row. Now that I have matrices with multiple rows I don't know how to make it sort from left to right. Right now it sorts from top to bottom.
For example, the third matrix goes
4 7
5 8
6 9
I need it to go
4 5
6 7
8 9
 m=numel(a); 
 for j=1:1:(m-1)
    for i=1:1:(m-1)
        if a(i)>a(i+1)
            temp=a(i);
            a(i)=a(i+1);
            a(i+1)=temp;
        end
    end
end
These are some of the matrices I need to work with
 a=[3 -2 1;4 0 5;1 2.2 -3]
 a=[4 2 1;9 3 5]
 a=[9 8;7 6;5 4]
Any help is appreciated. Thank you.
0 个评论
回答(2 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


