sort a matrix depending on first row

I have a matrix B
>> B
B =
5.0000 1.0000 6.0000 3.0000
0.6076 0.4943 0.1097 0.4754
-0.1178 -1.4831 1.1287 1.1741
0.6992 -1.0203 -0.2900 0.1269
0.2696 -0.4470 1.2616 -0.6568
depending on the first rwo values I want to sort the data. i.e., my result should look like this
>>
ans=
1.0000 3.0000 5.0000 6.0000
0.4943 0.4754 0.6076 0.1097
-1.4831 1.1741 -0.1178 1.1287
-1.0203 0.1269 0.6992 -0.2900
-0.4470 -0.6568 0.2696 1.2616
How to do this?

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2011 年 9 月 17 日
編集済み: Stephen23 2019 年 4 月 11 日

13 投票

out = sortrows(B.',1).'
variant
[~,inx]=sort(B(1,:));
out = B(:,inx);

3 件のコメント

manoj saladi
manoj saladi 2019 年 4 月 11 日
Hi,
Thank you very much, solved my problem
Barrett Durtschi
Barrett Durtschi 2020 年 8 月 3 日
Thank you for this answer! What would I change in the statement if I wanted the first four row values?
Bruno Luong
Bruno Luong 2020 年 8 月 3 日
out = sortrows(B(1:4,:).',1).'

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

TAB
TAB 2011 年 9 月 17 日

0 投票

[mat inx]=sort(b(1,:));
SortedMat=[b(:,inx(1)),b(:,inx(2)),b(:,inx(3)),b(:,inx(4))];

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

質問済み:

2011 年 9 月 17 日

コメント済み:

2020 年 8 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by