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

2 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by