sort the matrix X, then sort the another matrix in the same order as X

3 views (last 30 days)
Phoenix
Phoenix on 13 Dec 2021
Commented: Phoenix on 14 Dec 2021
I have a matrix X which of size 100 by 100, I had sorted the rows of matrix X in the descending order so that the maximum in each row stays in the first column and store it in a new matrix Y.
X = rand(100,100);
[Y, indx] = sort(X,2,'descend');
P = rand(100,100);
I need to use the same sorting order used while sorting X in matrix P and store it in matrix Q. I had tried using the code,
Q = P(indx);
This makes the sorting indices in the columns instead of rows. I need the sorting to be implemented in respective rows of P.
Thanks in advance!

Accepted Answer

dpb
dpb on 13 Dec 2021
Matrix expansion of subscripts gets you here...
Q=cell2mat(arrayfun(@(i) P(i,indx(i,:)),[1:size(P,1)].','UniformOutput',false));

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by