How to sort a cell array based on the first row?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dongyan Zhu
el 18 de Jun. de 2021
Comentada: Dongyan Zhu
el 18 de Jun. de 2021
I have two rows of cell array:
Combined =
2×3 cell array
{'4'} {'1'} {'6'}
{'1'} {'3'} {'4'}
Now I want to sort the first row of this 2*3 cell array from small number to large number and the second row will be sorted according to the first row, which means the second row should look like this:
{'1'} {'4'} {'6'}
{'3'} {'1'} {'4'}
Then I want to extract the second row as an 1*3 cell array.
Can someone help me? Many thanks.
0 comentarios
Respuesta aceptada
the cyclist
el 18 de Jun. de 2021
Out of curiosity, is there some reason you are using a cell array rather than a numeric array to store these numeric data?
% Original data
in = {'4','1','6';'1','3','4'}
% Sort first row, and get sorting index
[in_sorted,sorting_idx] = sort(in(1,:))
% Extract last row, sorted according to first
out = in(2,sorting_idx)
Más respuestas (0)
Ver también
Categorías
Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!