How to sort rows of a matrix without using sortrows function?

8 ビュー (過去 30 日間)
yijie yu
yijie yu 2019 年 4 月 11 日
編集済み: Stephen23 2019 年 4 月 11 日
Write a function to realize the function of sortrows.
I wrote a simple sort function, but our teacher said that we can use less than five lines of code to achieve, I do not know how to improve
function A = sortrows( A )
[m,n]=size(A);
for N=1:m-1
for M=N+1:m
if A(N,1)>A(M,1)
t=A(N,:);
A(N,:)=A(M,:);
A(M,:)=t;
else if A(N,1)==A(M,1)
ll=0;
rr=0;
k=1;
while k<n
ll=ll+A(N,k);
rr=rr+A(M,k);
if(ll>rr)
t=A(N,:);
A(N,:)=A(M,:);
A(M,:)=t;
end
end
end
M=M+1;
end
N=N+1;
end
disp(A);
end
  3 件のコメント
yijie yu
yijie yu 2019 年 4 月 11 日
I've already posted it up, and I'd like to get some Suggestions for simplifying functions
Stephen23
Stephen23 2019 年 4 月 11 日
編集済み: Stephen23 2019 年 4 月 11 日
@yijie yu: are you allowed to use sort ? If so, then you can implement a simple sortrows function with one loop and a few lines of code.

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

回答 (1 件)

Raj
Raj 2019 年 4 月 11 日
How about doing a small search and putting a little effort before dumping your homework question here!
See this.
  1 件のコメント
yijie yu
yijie yu 2019 年 4 月 11 日
I wrote a simple sort function, but our teacher said that we can use less than five lines of code to achieve, I do not know how to improve

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by