Clear Filters
Clear Filters

Four of 3D points: How to find 1 of them closest to a given point?

6 views (last 30 days)
A structure variable measurement contains cloud of 4 points with coordinates (x,y,z).
Distance d between any two points (X,Y,Z) and (x,y,z) is d= Sqrt[(X-x)^2 + (Y-y)^2 + (Z-z)^2].
Now there are a 100 000 entries in a table simulation, each entry is some point in a space, in no specific order. Given any point (X,Y,Z) from the simulation find the nearest point from the cloud of points in variable measurement. An index (1, 2, 3, or 4) is sufficient.
So far, I am using for loop going from over all of the rows and calling function findNearestNeighbor, which requires additional Matlab toolbox. After every loop I receive the index of closest point inside the cloud and append it to an array closestPoint. After the loop is finished, the table measurement receives the fourth column indexOfClosesPoint.
My problem is that the loop is time consuming, because the calling findNearestNeighbor here is the bottleneck. How would you
  1. find the closest distance from each of 100 000 points to the cloud of 4 points time efficiently. findNearestNeighbor is doing its work, but maybe there is way to do it simplier
  2. store the indices (1, 2, 3, or 4) into the table simulation and wirte it in an text file.
The structure variable measurement and the table simulation you will find in the attachment.
closestPoint = [];
for iRow = 1:height(measurement)
best = findNearestNeighbor(simulation.x(iRow),...
simulation.y(iRow),...
simulation.z(iRow), measurement);
closestPoint(end+1) = best;
end
simulationNew = addvars(simulation, closestPoint.',
'NewVariableNames', {'indexOfClosesPoint'});
function in a separate file findNearestNeighbor.m:
function indices = findNearestNeighbor(x, y, z, measurementValues)
a = [vertcat(measurementValues.x), vertcat(measurementValues.y),...
vertcat(measurementValues.z)];
ptCloud = pointCloud(a); %Create a point cloud object of measurement data
point = [x, y, z]; %Specify a point from simulation
K = 1; % Specify the number of nearest neighbors to be identified
[indices,~] = findNearestNeighbors(ptCloud,point,K);
end

Answers (1)

Matt J
Matt J on 29 Jun 2021
Edited: Matt J on 29 Jun 2021
XYZ=[simulation.x(:), simulation.y(:),simulation.z(:)];
xyz=[measurementValues.x(:), measurementValues.y(:),measurementValues.z(:)];
[D,indices] = pdist2(xyz,XYZ,'euclidean','Smallest',1);
  2 Comments
Nazar Adamchuk
Nazar Adamchuk on 29 Jun 2021
Instead of
xyz=[measurementValues.x(:), measurementValues.y(:),measurementValues.z(:)];
must be
xyz = [vertcat(measurement.x), vertcat(measurement.y), vertcat(measurement.z)];
I reuploaded the examples for you as well that we are talking about the same thing. So basically, if I run the whole script:
XYZ = [simulation.X(:), simulation.Y(:),simulation.Z(:)];
xyz = [vertcat(measurement.x), vertcat(measurement.y), vertcat(measurement.z)];
[D,indices] = pdist2(xyz,XYZ,'euclidean','Smallest',1);
The array of the indices shows me
2, 2, 2, 2, 2, 2, 2, 2, 2, 2
It means that for all of the simulation points is the measurement value 2 is the nearest. I was suprises and put this value far away from all of the them
measurement(2).x = 7337.900000000000000; measurement(2).y = 843543.700000000000000;
and I have still the same:
2, 2, 2, 2, 2, 2, 2, 2, 2, 2
Do you know what is worng?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!