Ordering a matrix as x increases and y increases

1 次查看(过去 30 天)
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:

采纳的回答

Voss
Voss 2023-10-25
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 个评论
Alberto Acri
Alberto Acri 2023-10-26
Thank you @Voss! What if I wanted to modify the 'square' matrix like this way instead?
Voss
Voss 2023-10-26
编辑:Voss 2023-10-26
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by