Ordering a matrix as x increases and y increases

1 vue (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 25 Oct 2023
Modifié(e) : Voss le 26 Oct 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:

Réponse acceptée

Voss
Voss le 25 Oct 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 commentaires
Alberto Acri
Alberto Acri le 26 Oct 2023
Thank you @Voss! What if I wanted to modify the 'square' matrix like this way instead?
Voss
Voss le 26 Oct 2023
Modifié(e) : Voss le 26 Oct 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by