How to select, or filter, the external border/boundary in a set of (x,y)-points?

조회 수: 7 (최근 30일)
Given two or more set of points (please see attached, as example, two sets of points called "b1" and "b2", that are stored in "borders.mat"), how can I select or filter the common external border/boundary from the union of those point datasets?
After joining the two datasets, I would like to keep only the points that represent the total common external border (i.e. removing any common set of points or any "internal" point). Here, in this example, I show only two set of points, but I have many of them.
load("borders.mat")
hold on
plot(b1(:,1),b1(:,2))
plot(b2(:,1),b2(:,2))

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 7월 10일
load("borders.mat");
%Original figure
plot(b1(:,1),b1(:,2))
hold on
plot(b2(:,1),b2(:,2))
%Obtain coordinates corresponding to outside border by performing
%exclusive OR on the data
z=setxor(b1,b2,'rows','stable');
%'stable' option is to maintain the order of the coordinates
%plot the boundary
figure
plot(z(:,1),z(:,2),'k')

추가 답변 (1개)

MarKf
MarKf 2023년 7월 10일
As in
borders = load(websave('rd', "https://nl.mathworks.com/matlabcentral/answers/uploaded_files/1430803/borders.mat"));
b1 = borders.b1; b2 = borders.b2;
idx_common = ismember(b1,b2,'rows');
coordsincommon = b1(idx_common,:); %169
It seems that they are the exact same coordinates, so this works, tho I haven't checked if it captures them all, otherwise there is also ismembertol
  댓글 수: 2
Sim
Sim 2023년 7월 11일
Hi @MarKf! Thanks for your reply! I tried to compare both methods here proposed, but, unless I misinterpreted your method or I did something wrong, I do not get the desired result.... :-)
% Load data
borders = load(websave('rd', "https://nl.mathworks.com/matlabcentral/answers/uploaded_files/1430803/borders.mat"));
b1 = borders.b1; b2 = borders.b2;
% Dyuman Joshi's Method
c=setxor(b1,b2,'rows','stable');
plot(c(:,1),c(:,2),'r')
% MarKf's Method
d=b1(ismember(b1,b2,'rows'),:);
plot(d(:,1),d(:,2),'b','linewidth',2)
MarKf
MarKf 2023년 8월 7일
Yeah the accepted answer is what you want more straightfowardly, setxor being the opposite of ismember (very useful but there is no tolerance version).
I showed how you can get those common points, answering your specific "how can I select [or filter] the common external border/boundary from the union of those point datasets?" query, you could've then used the index provided (idx_common) to filter them out. You plotted only the common boundary points directly (last point seem to get captured first or viceversa).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by