Four of 3D points: How to find 1 of them closest to a given point?
조회 수: 9 (최근 30일)
이전 댓글 표시
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
- 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
- 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
댓글 수: 0
답변 (1개)
Matt J
2021년 6월 29일
편집: Matt J
2021년 6월 29일
XYZ=[simulation.x(:), simulation.y(:),simulation.z(:)];
xyz=[measurementValues.x(:), measurementValues.y(:),measurementValues.z(:)];
[D,indices] = pdist2(xyz,XYZ,'euclidean','Smallest',1);
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulated Annealing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!