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

5 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by