Borrar filtros
Borrar filtros

Ordering a matrix as x increases and y increases

1 visualización (últimos 30 días)
Alberto Acri
Alberto Acri el 25 de Oct. de 2023
Editada: Voss el 26 de Oct. de 2023
Hi. I need to remove repeating nodes inside the 'square' array (I need to get only unique rows without repetition).
I tried with the 'unique' command which should be the correct solution, but I would like to confirm.
load square.mat
square = unique(square, 'rows');
figure
plot(square(:,1),square(:,2),'g.','Markersize',15);
axis equal
set(gca, 'YDir','reverse')
Then I need to sort the matrix so that I have the coordinates in the following order:

Respuesta aceptada

Voss
Voss el 25 de Oct. de 2023
square = unique(square,'rows'); works to remove repeated rows, yes.
In I uderstand the ordering you want, it is to sort by y-coordinate (increasing) and where y-coordinates are equal sort by x-coordinate (increasing). You can sort square in this manner using square = sortrows(square,[2 1]);
load square
square = unique(square,'rows');
square(1:5,:)
ans = 5×2
-89.7000 -108.0000 -89.7000 -107.5781 -89.7000 -107.1563 -89.7000 -106.7344 -89.7000 -106.3125
square = sortrows(square,[2 1]); % sort by y, then by x
square(1:5,:)
ans = 5×2
-89.7000 -108.0000 -89.2781 -108.0000 -88.8562 -108.0000 -88.4344 -108.0000 -88.0125 -108.0000
  2 comentarios
Alberto Acri
Alberto Acri el 26 de Oct. de 2023
Thank you @Voss! What if I wanted to modify the 'square' matrix like this way instead?
Voss
Voss el 26 de Oct. de 2023
Editada: Voss el 26 de Oct. de 2023
load square
square = unique(square,'rows');
square(1:5,:)
ans = 5×2
-89.7000 -108.0000 -89.7000 -107.5781 -89.7000 -107.1563 -89.7000 -106.7344 -89.7000 -106.3125
square = sortrows(square,[-2 1]); % sort by y (decreasing), then by x (increasing)
square(1:5,:)
ans = 5×2
-89.7000 107.5781 -89.2781 107.5781 -88.8562 107.5781 -88.4344 107.5781 -88.0125 107.5781

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.

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