Ordering of the columns of an array according to a given line

1 回表示 (過去 30 日間)
Marcus Vinicius Pereira de Souza
Marcus Vinicius Pereira de Souza 2012 年 9 月 24 日
Dear all, I have the following database:
15 50 15 30 20
1 3 5 2 4
However, I need to sort as follows:
15 30 50 20 15
1 2 3 4 5
Can anyone help me? Thanks a lot. Marcus Vinicius

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 9 月 24 日
編集済み: Andrei Bobrov 2012 年 9 月 24 日
A = [15 50 15 30 20
1 3 5 2 4]; The initial array
[ii ii] = sort(A(2,:));
out = A(:,ii);
or
out = sortrows(A',2)';

Rodrigo
Rodrigo 2012 年 9 月 24 日
It seems like you want to sort by the value of the second row. To do this you first find the order you want:
[~,ind]=sort(A(2,:));
where A is the array containing your data. Then you produce the sorted array by calling the index:
B=A(:,ind);

カテゴリ

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