54 views (last 30 days)

I have two matrices, A (96100x3) and B(16416x3). I need to subtract the XYZ coordinates defined by the columns of B from those of A. But as they are of different resolutions, the difference or setxor is still giving sort of union of these two matrices.

Is there any way I can subtract the region bounded by B from A?

TIA

Adam Danz
on 23 Jan 2021

Edited: Adam Danz
on 23 Jan 2021

I think what you're asking is how to remove rows of A that are nearly equal to rows of B. Removal is different from subtraction and setxor does the former, not the latter.

The question is, what do you define as "near"?

I used pdist2() to compute the pairwise distances between all points in A and B and there is no obvious cluster of distances around 0 (note, pdist2 will be slow due to large variable sizes!).

% Read-in the data

A = readmatrix('A.xlsx');

B = readmatrix('B.xlsx');

% Compute pair-wise distance.

% This will probably take several seconds or more! size: 96100 x 16416

pd = pdist2(A,B);

% Plot distribution of pair-wise distances

figure()

histogram(pd(:), 20)

xlabel('paired distance')

ylabel('count')

title('All points')

% Show +/- 1 std

mu = mean(pd(:));

sd = std(pd(:));

xline(mu-sd,'k-')

xline(mu+sd,'k-')

When we look at distances around 0:.2, there is still no clear cluster that would support an objective definition of "near".

figure()

histogram(pd(pd<.2))

xlabel('paired distance')

ylabel('count')

title('Points at distances less than 0.2')

The minimum distance is

min(pd(:))

% ans =

% 0.000316512059369112

If there were a clear definition of "near" you could use that as a threshold to detect and remove "near" coordinates from the pd pair-wise distance matrix. But that isn't clear from the data.

If, on the other hand, you're looking for coordinates that are nearly 1 unit apart in distanced, then the data above do suggest a clear, more-objective way to detect and eliminate paired coordinates at nearly 1-unit distance, though you'll still need to subjectively select a threshold distance from 1.

For example, any pairwise distance within 0.05 of 1 identifies a row of A to be removed.

rmIdx = abs(pd-1) < .05;

A(any(rmIdx,2)) = [];

Image Analyst
on 24 Jan 2021

There can be no well defined threshold because it's a judgment call.

Anyway, pdist2() will not tell you if the points of A are inside the outer shell of points B - it only tells you if they're close, which is not the same thing. For example if the points of A were a shell of radius 20 plus one point at the very center, and the points of B were a shell of radius 19, pdist2() will not tell you that the point of A that's at the center is inside point set B.

You'll have to use

and then use the isInterior method to determine if the points in A are inside the Delaunay triangulation of set B, and if they are, remove them.

Adam Danz
on 24 Jan 2021

The idea is to minimized any subjective decisions by using the data to set thresholds. This requires exploration of the data.

Here's another visualization of your data zoomed into the section of overlap between A (blue o) and B (red x). This view is from the "top" where each marker is actually a column of markers in 3D.

A = readmatrix('A.xlsx');

B = readmatrix('B.xlsx');

plot3(A(:,1), A(:,2), A(:,3), 'o')

hold on

plot3(B(:,1), B(:,2),B(:,3), 'rx')

view(2)

axis equal

xlim([-.05 .25])

ylim([.33 .88])

This is only a small portion of your entire dataset.

And with this view we also see that the mean distance of 1 between points in A and B is irrelevant because you're only concerned with the portion of overlap and those distances a lot closer than 1.

What you need to do is repeat the pdist2 calculations but only for the section of overlap. That will greatly reduce the number of data points and will prevent the maximum array size error you got.

- Get the min & max values for X and Y coordinates in B
- Isolate the section of A based on those ranges (add a little space)
- Repeat the pdist2 calculations I showed you and plot the distribution of distances. The distribution should be much closer to 0.

Image Analyst
on 24 Jan 2021

"For instance let A correspond to the point cloud corresponding to a sphere and B correspond to a smaller, concentric sphere. The result (equivalent to setxor) that I am seeking is a hollow sphere, that is a resultant of subracting the smaller sphere from the larger sphere."

Sounds like you want convhulln(). Use it on one cloud and then test all the points of one to see if the point is inside the convex hull of the other.

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281167

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281167

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281232

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281232

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281237

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281237

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281282

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281282

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281397

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281397

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281432

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/724738-subtracting-xyz-coordinates-defined-by-matrices#comment_1281432

Sign in to comment.