# knnsearch but with a constraint

3 次查看（过去 30 天）
Pelajar UM2021-10-29

I have an 11,000x3 array representing x,y,z coordinates of points in 3D space. I want to find the nearest neighbor using knnsearch but with this constraint that the normal vector associated with any pair of points should form an angle larger than 170 degree (I have already calculated the normals).
How can I include this constraint in my knnsearch?
Here's the code so far and I'm trying to use a while loop until the condition is met, but pretty sure, this is not the right way of doing it:
[Idx] =knnsearch (points,points,'K',2, 'Distance',"chebychev"); % find the second nearest point (first point is the point itself)
Idx2=Idx(:,2) %take the index from the second point
sortedN=N(Idx2,:) %sort the normals based on the new inex
angle = transpose (rad2deg (atan2(vecnorm(cross(N',sortedN')), dot(N',sortedN')))) %calculate the angle between the original normal and the sorted normal
% and this is clearly not how one should do a while loop because it doesn't do anything when I run it.... but basically
% I'm trying to tell it to search for the next nearest point until the
% condition is met.
while angle < 170
[Idx, distance] =knnsearch (points,points,'K',i, 'Distance',"chebychev");
i+1
end
% I want to update new index and distance value at the end

### 回答（1 个）

KSSV 2021-10-29
You can specify the number of points you want from knnsearch. Get as many number of points you want. After calculate the angle and pick the points you want.
I would suggest you to have a look on rangesearch as well.
##### 3 个评论显示隐藏 2更早的评论
Pelajar UM 2021-10-29
hmm, like this? I think I'm still missing something.
while angle < 170
[Idx, dist] =knnsearch (centroid,centroid, 'K',i, 'Distance',"chebychev");
i+1
sortedcentroid = centroid(Idx,:)
sortedN=N(Idx,:)
angle = transpose (rad2deg (atan2(vecnorm(cross(N',sortedN')), dot(N',sortedN'))))
end

### Community Treasure Hunt

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

Start Hunting!