How to 'carry' matrix index with matrix operations

1 visualización (últimos 30 días)
Xiaohan Du
Xiaohan Du el 15 de Nov. de 2016
Editada: Roger Stafford el 15 de Nov. de 2016
Hi all,
I'd like to keep the matrix index while carrying out matrix operations, for example:
a = randi([1 20], 1, 5)';
indx = find(a);
c = [indx a];
b = sort(a);
I obtain:
a = [15 10 2 5 19]'; c = [1 2 3 4 5; 15 10 2 5 19]'; b = [2 5 10 15 19]';
After sort, how can I keep the old correspondence of index and vector in new matrix b? i.e. I want b to be something like this:
b = [3 4 2 1 5; 2 5 10 15 19]';
Thanks!

Respuesta aceptada

Roger Stafford
Roger Stafford el 15 de Nov. de 2016
Editada: Roger Stafford el 15 de Nov. de 2016
Use the second quantity returned by the 'sort' function:
[b,p] = sort(a);
The 'p' is an index for rearranging the first row from 1,2,3,4,5 to 3,4,2,1,5 with indx(p).
  1 comentario
Xiaohan Du
Xiaohan Du el 15 de Nov. de 2016
Thanks Roger! I should have checked the function help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by