Sorting

8 ビュー (過去 30 日間)
CHAITANYA PRASAD
CHAITANYA PRASAD 2012 年 3 月 30 日
I have 'line' matrix as follows :
line(1,m)= 1 2 3 4 5 6 7 8 9 10
line(2,m)=1.3 -2.3 4.245 12.3 -4.5 9.43 8.34 -5.3 3.54 6.4
I want to sort 2nd row in descend order, but 1st row as follows:
line(1,m)= 4 6 7 10 3 9 1 2 5 8
line(2,m)=12.3 9.43 8.34 6.4 4.245 3.54 1.3 -2.3 -4.5 -5.3

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 3 月 30 日
l = [1 2 3 4 5 6 7 8 9 10
1.3 -2.3 4.245 12.3 -4.5 9.43 8.34 -5.3 3.54 6.4];
solution
[i1,i1] = sort(l(2,:),'descend');
out = l(:,i1)
OR
out = sortrows(l.',-2).'
  1 件のコメント
CHAITANYA PRASAD
CHAITANYA PRASAD 2012 年 4 月 1 日
Thank u Bobrov...It was very helpful..:)

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


C.J. Harris
C.J. Harris 2012 年 3 月 30 日
line(1,:)= [1 2 3 4 5 6 7 8 9 10];
line(2,:)= [1.3 -2.3 4.245 12.3 -4.5 9.43 8.34 -5.3 3.54 6.4];
[line(2,:), line(1,:)] = sort(line(2,:),'descend');
You also shouldn't really use 'line' as a variable name, since this is also an inbuild function, and might cause confusion if you don't clear the variable after use.
  1 件のコメント
CHAITANYA PRASAD
CHAITANYA PRASAD 2012 年 4 月 1 日
Thank u Chris...Actually i was using linerank...just for example i gave line...and it was very useful...:)

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

カテゴリ

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