필터 지우기
필터 지우기

Ordering a matrix as x increases and y increases

조회 수: 1 (최근 30일)
Alberto Acri
Alberto Acri 2023년 10월 25일
편집: Voss 2023년 10월 26일
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