How to sort a matrix using loops?
4 次查看(过去 30 天)
显示 更早的评论
I need to sort a matrix from least to greatest without the sort function. It needs to be done in nested loops. For example, I need to take the matrix [9 5;7 4;8 6] and sort it into [4 5;6 7;8 9].
1 个评论
Roger Stafford
2016-4-17
See this site for a variety of possible algorithms you might use:
https://en.wikipedia.org/wiki/Sorting_algorithm
回答(1 个)
John BG
2016-4-17
编辑:John BG
2016-4-17
Michael
1 for loop is enough
A=randi([-10 10],5,6) % generate input
B=A(:)' % reshape A into straight line
L=length(B)
C=[]
for k=1:L
v=find(B==max(B)) % in case repeated values
C=[C B(v(1))]
B(v(1))=[]
end
If you find this answer of any help solving your question, please click on the thumbs-up vote link, or mark it as accepted answer
thanks in advance
John
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!