How to sort the rows of an array according to another vector?

9 ビュー (過去 30 日間)
Cantor Set
Cantor Set 2020 年 2 月 18 日
回答済み: Star Strider 2020 年 2 月 18 日
If I have an array D:
D=[1 1 0 1 0 1; 4 6 7 8 9 9; 1 1 1 2 3 4]; b=[2 44 0];
each row in D crossponds to a number in b:
the first row crossponds to 2
the second row crossponds to 44
the third row crossponds to 0
I want to sort b in a descending order and according to the sorted vector b the rows of the array D are arranged such that
D=[4 6 7 8 9 9; 1 1 0 1 0 1; 1 1 1 2 3 4]; bb=sort(b,'descend');

採用された回答

Star Strider
Star Strider 2020 年 2 月 18 日
Try this:
D=[1 1 0 1 0 1; 4 6 7 8 9 9; 1 1 1 2 3 4];
b=[2 44 0];
[bb,idx] = sort(b,'descend');
Out = D(idx,:)
producing:
Out =
4 6 7 8 9 9
1 1 0 1 0 1
1 1 1 2 3 4

その他の回答 (0 件)

カテゴリ

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