Borrar filtros
Borrar filtros

Sort first column of a 5x2 matrix but keep the rows intact after sorting

28 visualizaciones (últimos 30 días)
Hi, I have a 5x2 matrix and I would like to sort the first column and keep the rows together after the sorting is done. For example, I have an array given by
UP = zeros(5,2)
ai = [13,11,15,26,20]
for i = 1:5
UP(i,1) = ai;
UP(i,2) = i;
end
After I sort the first column of the array, I would like to keep the second column assigned with the value of the first column. I would like an output that looks like the following
11,2
13,1
15,3
20,5
26,4

Respuesta aceptada

Stephen23
Stephen23 el 15 de Mzo. de 2019
Editada: Stephen23 el 15 de Mzo. de 2019
>> V = [13,11,15,26,20];
>> M = [V;1:numel(V)].'
M =
13 1
11 2
15 3
26 4
20 5
>> Z = sortrows(M,1)
Z =
11 2
13 1
15 3
20 5
26 4

Más respuestas (0)

Categorías

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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by