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

5 vues (au cours des 30 derniers jours)
Phoenix
Phoenix le 13 Déc 2021
Commenté : Phoenix le 14 Déc 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!

Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by