Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find intersection between two data sets

Tags:

arrays

matlab

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.

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

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?

enter image description here

like image 639
Trippy Avatar asked Jun 09 '16 05:06

Trippy


People also ask

How do you calculate intersection?

The intersection count is the number of times polylines from one feature class intersect other polylines or polygons in either the same or another feature class. For example, two road features are permitted to intersect once, but not more than once.

How do I find the intersection in Excel?

Excel can help to automate the task of finding the intersection point of two lines by using the =slope() and =intersection() function and replacing their values with the given equations.

How do you find the intersection of two plots?

When the graphs of y = f(x) and y = g(x) intersect , both graphs have exactly the same x and y values. So we can find the point or points of intersection by solving the equation f(x) = g(x). The solution of this equation will give us the x value(s) of the point(s) of intersection.

How do you find the intersection of two sets in Matlab?

C = intersect( A,B ) returns the data common to both A and B , with no repetitions. C is in sorted order. If A and B are tables or timetables, then intersect returns the set of rows common to both tables.


1 Answers

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

Full code :

[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')

Output :

enter image description here

EDIT - A 2D example :

[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');

Output :

enter image description here

Edit 2 :

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

like image 77
BillBokeey Avatar answered Oct 04 '22 17:10

BillBokeey