Sorting one matrix with another matrix

Hi everyone,
I have a issue regarding matlab
Basically, I have a matrix A with data and the corresponding return in another matrix called B
What I want to do is to sort (from min to max) my first matrix A and to get a matrix C with the corresponding sorted value of matrix B
For example if
A is
2 3
4 1
sorted, A becomes
A_sorted
2 1
4 3
and B
9 4
8 9
I would like to get
C
9 9
8 4
Could you help me figure out how to do it?
Thanks

回答 (2 件)

James Tursa
James Tursa 2016 年 10 月 27 日
編集済み: James Tursa 2016 年 10 月 27 日

0 投票

[A_sorted,X] = sort(A);
Y = repmat(1:size(A,2),size(A,1),1);
C = B(sub2ind(size(A),X,Y));
Alexandra Harkai
Alexandra Harkai 2016 年 10 月 27 日

0 投票

[A_sorted, idx]=sort(A,1)
A_sorted =
2 1
4 3
idx =
1 2
2 1
B(bsxfun(@plus,idx,size(A,1)*(0:(size(A,2)-1))))
sort(.,1) gives the indices for sorting, which can then be used to re-order B. Using linear indexing for which we need to add 0 to the first column of idx, 2 to the second, and so on...

カテゴリ

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

質問済み:

2016 年 10 月 27 日

編集済み:

2016 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by