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

3 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by