# knnsearch but with a constraint

조회 수: 1(최근 30일)
Pelajar UM 2021년 10월 29일
편집: Pelajar UM 2021년 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!