# How to sort out the matrix based on 1st column?

153 visualizaciones (últimos 30 días)
Sky Scrapper el 30 de En. de 2019
Editada: Stephen23 el 31 de En. de 2019
Hello,
I have matrix, A= [ -95 0 1 0 1 0 1 0 1 1; -95 0 1 0 1 0 1 0 1 -1 ; 0 0 1 0 1 1 0 1 0 1 ; 0 0 1 0 1 1 0 1 0 -1 ; -76 0 1 1 0 0 1 0 1 1 ; -76 0 1 1 0 0 1 0 1 -1 ; 76 0 1 1 0 1 0 1 0 1 ; 76 0 1 1 0 1 0 1 0 -1 ; -76 1 0 0 1 0 1 0 1 1 ; -76 1 0 0 1 0 1 0 1 -1 ; 95 1 0 1 0 1 0 1 0 1 ; 95 1 0 1 0 1 0 1 0 -1 ];
I want to sort out the matrix in ascending order based on the 1st column but the condition is I will have to put 1st and 2nd rows together, 3rd and 4rth rows together and so on...Finally, after sorting I want to get,
B = [95 1 0 1 0 1 0 1 0 1; 95 1 0 1 0 1 0 1 0 -1; 76 0 1 1 0 1 0 1 0 1; 76 0 1 1 0 1 0 1 0 -1; 0 0 1 0 1 1 0 1 0 1; 0 0 1 0 1 1 0 1 0 -1; -76 0 1 1 0 0 1 0 1 1; -76 0 1 1 0 0 1 0 1 -1; -76 1 0 0 1 0 1 0 1 1 ;-76 1 0 0 1 0 1 0 1 -1; -95 0 1 0 1 0 1 0 1 1; -95 0 1 0 1 0 1 0 1 -1];
Look, in matrix A we have the value '-76' was repeated 4 times in 5th, 6th, 9th and 10 th rows. My condition is after 5th row, there must be 6th row. Also after 9th rows there must be 10 th row. The same will happen in case of other rows (e.g: for '-95' after 1st row there must be 2nd row, for '0' the after 3rd row it must be 4th row).
I find it is difficult to do. Could anyone please help me?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuesta aceptada

Stephen23 el 30 de En. de 2019
Editada: Stephen23 el 31 de En. de 2019
"How to sort out the matrix based on 1st column?"
The sortrows function is stable, in the sense that the order of any two rows will be the same after sorting if the rows have the same sorted values. So if you sort by only the first column AND rows one and two have the same value in the first column, then they will be in the same order in the output. This makes your task easy:
>> sortrows(A,-1)
ans =
95 1 0 1 0 1 0 1 0 1
95 1 0 1 0 1 0 1 0 -1
76 0 1 1 0 1 0 1 0 1
76 0 1 1 0 1 0 1 0 -1
0 0 1 0 1 1 0 1 0 1
0 0 1 0 1 1 0 1 0 -1
-76 0 1 1 0 0 1 0 1 1
-76 0 1 1 0 0 1 0 1 -1
-76 1 0 0 1 0 1 0 1 1
-76 1 0 0 1 0 1 0 1 -1
-95 0 1 0 1 0 1 0 1 1
-95 0 1 0 1 0 1 0 1 -1
Or perhaps (based on the pattern in the tenth column):
>> sortrows(A,[-1,-10])
ans =
95 1 0 1 0 1 0 1 0 1
95 1 0 1 0 1 0 1 0 -1
76 0 1 1 0 1 0 1 0 1
76 0 1 1 0 1 0 1 0 -1
0 0 1 0 1 1 0 1 0 1
0 0 1 0 1 1 0 1 0 -1
-76 0 1 1 0 0 1 0 1 1
-76 1 0 0 1 0 1 0 1 1
-76 0 1 1 0 0 1 0 1 -1
-76 1 0 0 1 0 1 0 1 -1
-95 0 1 0 1 0 1 0 1 1
-95 0 1 0 1 0 1 0 1 -1
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Sky Scrapper el 30 de En. de 2019
woww..how simpler thought to solve the problem. It's working! Thanks a lot.

Iniciar sesión para comentar.

### 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!

Translated by