Filter löschen
Filter löschen

Ordering a matrix as x increases and y increases

1 Ansicht (letzte 30 Tage)
Alberto Acri
Alberto Acri am 25 Okt. 2023
Bearbeitet: Voss am 26 Okt. 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:

Akzeptierte Antwort

Voss
Voss am 25 Okt. 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 Kommentare
Alberto Acri
Alberto Acri am 26 Okt. 2023
Thank you @Voss! What if I wanted to modify the 'square' matrix like this way instead?
Voss
Voss am 26 Okt. 2023
Bearbeitet: Voss am 26 Okt. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by