查找两个数据集之间的交集 [英] Find intersection between two data sets

查看:410
本文介绍了查找两个数据集之间的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成两个与此类似的数组:

I am generating two arrays similar to this:

[x,y,z] = sphere;
A=[x,y,z]
B=[x+0.5,y+0.5,z+0.5]

第二个数组与第一个数组偏移.

The second array is at an offset to the first.

我想找到这两个数组A和B的交集空间.

I would like to find the intersection space of both of these arrays A and B.

在这种情况下,我使用了sphere函数,但是可以对任何两个不一定是球形的数据数组执行此操作.有办法吗?

I have used the sphere function in this case but can this be done for any two data arrays not necessarily spherical. Is there a way to do this?

我正在提供我要寻找的图像.我想找到这两个区域之间的交集.但是这些值不一定会与您看到的相同.

I am including an image for what I am looking for. I want to find the intersection between these two areas. But the values are not necessarily going to be the same as you can see.

如果我对每个空间的极限都有一个方程式,这会使问题变得更容易吗?

If I have an equation for the limits of each of the spaces, would that make the problem easier?

推荐答案

我在评论中指出,可以使用convhullinpolygon解决此问题,只有inpolygon似乎不适用于3D多边形.我们将使用delaunayTriangulationpointLocation以获得结果

I stated in the comments that one could use convhull and inpolygon to solve this problem, only inpolygon doesn't seem to apply to 3D polygons. We'll use delaunayTriangulation and pointLocation in order to get to the result

[x,y,z] = sphere;
A=[x(:),y(:),z(:)];
B=[x(:)+0.5,y(:)+0.5,z(:)+0.5];

tess1=delaunayTriangulation(A); % delaunay Triangulation of points set A
tess2=delaunayTriangulation(B); % delaunay Triangulation of points set B

Tmp=[A;B];

% Point location searches for the triangles in the given delaunay     
% triangulation that contain the points specified in Tmp, here Tmp is 
% the reunion of sets A and B and we check for both triangulations
ids1=~isnan(pointLocation(tess1,Tmp));
ids2=~isnan(pointLocation(tess2,Tmp));

% ids1&ids2 is a logical array indicating which points
% in Tmp are in the intersection
IntersectPoints=Tmp(ids1&ids2,:);


plot3(A(:,1),A(:,2),A(:,3),'+b'); hold on
plot3(B(:,1),B(:,2),B(:,3),'+g');
plot3(IntersectPoints(:,1),IntersectPoints(:,2),IntersectPoints(:,3),'*r')

输出:

[x,y,z] = sphere;
A=[x(:),y(:)];
B=[x(:)+0.5,y(:)+0.5];

tess1=delaunayTriangulation(A); % delaunay Triangulation of points set A
tess2=delaunayTriangulation(B); % delaunay Triangulation of points set B

Tmp=[A;B];

% Point location searches for the triangles in the given delaunay     
% triangulation that contain the points specified in Tmp, here Tmp is 
% the reunion of sets A and B and we check for both triangulations
ids1=~isnan(pointLocation(tess1,Tmp));
ids2=~isnan(pointLocation(tess2,Tmp));

% ids1&ids2 is a logical array indicating which points
% in Tmp are in the intersection
IntersectPoints=Tmp(ids1&ids2,:);


plot(A(:,1),A(:,2),'+b'); hold on
plot(B(:,1),B(:,2),'+g');
plot(IntersectPoints(:,1),IntersectPoints(:,2),'*r');

输出:

如果您希望代码自动适应2D或3D阵列,则只需要修改绘图调用即可.只需编写一个if语句,该语句将检查A和B中的列数

If you want your code to adapt to either 2D or 3D arrays automatically, you just really need to modify the plot calls. Just write an if statement that will check the number of columns in A and B

这篇关于查找两个数据集之间的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆