Modify K nearest neighbors search (knnsearch) to ignore the point in question

5 Ansichten (letzte 30 Tage)
I am using knnsearch to find the index values of the nearset point to the point in question. My only problem is that I need to use knnsearch on the same matrix multiple times. So when I grab the first set of values using knnsearch, the second time around it grabs the point in question itself instead of the next new point. I tried splitting the points into different matrices, but the problem is that I need the true original index positions from the starting matrix. The issue begins at indx2 because the next point in the chain should be point 33, but matrix G only has 32 points since the size has shrunk. My goal is establish a master index of all the points' nearest neighbors from tip to endpoint. The tip of the chain would be the first column in a given row and the tip's nearest neighbor would be in the second column, and so on until the end. Here is my code so far.
clear
close all
clc
% A = 50 pts
A = [0,0,0;0.00977495601952172,0.0129188554738323,0.999868768093125;-0.566794094824837,-0.823750570492204,0.0133959578031223;0.0279587435128966,0.0380588867245362,1.99938731654588;0.830388266617646,0.583999369869120,0.978401571089338;-1.07826433834531,-1.64452537544960,0.267810795303313;0.168715496407312,0.263085998125572,2.96351922435162;-0.791458202545459,0.611268411797120,0.998070417818667;-1.41124221175034,-0.289407593850698,0.0506110237693017;1.69942938355116,1.04462646221878,1.15892918358242;-1.55216375501531,-2.44987966243271,0.623934110066297;0.188310075544404,0.501770043093754,3.93441879647330;-1.44603145623221,1.35107113546187,1.15371676572365;-2.35391403973316,0.0183196401577155,0.179737992978120;2.59879677816763,1.38804628951095,1.42948622326796;-1.92629974765512,-3.28302931738291,1.03122259685150;0.190461468181228,0.731318108759712,4.90771374511875;-2.01837467713375,2.14014570650778,1.37684130228734;-3.30531517848174,0.217844651708771,0.414313445558867;3.50709118523837,1.65551615551852,1.75113998255253;-2.23204460424917,-4.13488361866807,1.45650407075820;0.193343935566412,0.934427321909631,5.88686559182864;-2.51244231909718,2.97180232130260,1.63030617210704;-4.25650570961388,0.357419056329789,0.689550723301627;4.41845060685608,1.87363023778730,2.10021053665969;-2.47842156351806,-4.99324597672323,1.90651791078471;0.204283419234320,1.13357858075284,6.86677329350173;0.617793464714887,0.0559247730198438,6.10612769252045;-2.96417360323821,3.81704174520463,1.91580426879081;-5.20242439373799,0.476408999682868,0.991344090368801;5.32672360772571,2.06445068867013,2.47253796167180;-2.69346338350278,-5.85030685131976,2.37470982965291;0.218591936197924,1.32878562119851,7.84743096960627;-3.26247612265381,-4.91985596911837,1.29018005288354;1.20197539596291,-0.580347098756520,6.61000225666269;-3.37592827171665,4.67443410026770,2.22457026832058;-6.14584567995603,0.584770610127789,1.30473528056482;6.23299868743130,2.22581272192086,2.86321400922248;-2.89467385558292,-6.70726873056154,2.84918921113506;0.209717491582492,1.53184472980862,8.82655723451965;5.28440710514136,1.17097130314088,2.91964410258613;0.576278852478300,2.24513662818586,7.66753960818188;-4.04653068178957,-4.84646596151352,0.673842194982372;1.72465217643194,-1.18852390077965,7.20743841278270;-3.76500316313530,5.53715317053961,2.54758193166533;-7.08499357301506,0.666849489054822,1.63829830698843;7.13773556686645,2.38268963160310,3.25924533904186;-3.08850223839479,-7.56458389242937,3.32609724340970;0.176682672356017,1.77636612069925,9.79563823738556;5.70892694024996,0.283366758099107,3.09836478191717];
SizeA = size(A,1);
x = A(:, 1);
y = A(:, 2);
z = A(:, 3);
figure
plot(x, y, '.', 'color', 'k', 'Markersize', 12,'MarkerEdgeColor','w');
for ii = 1:SizeA
t = text(x(ii),y(ii),num2str(ii));
t.Color = 'k';
end
title('3D to 2D Mapping XY Plane: Index Numbers');
xlabel('x')
ylabel('y')
%%
figure
plot(x, z, '.', 'color', 'k', 'Markersize', 12,'MarkerEdgeColor','w');
for ii = 1:SizeA
t = text(x(ii),z(ii),num2str(ii));
t.Color = 'k';
end
title('3D to 2D Mapping XZ Plane: Index Numbers');
xlabel('x')
ylabel('z')
%%
dist1a = nan(numel(x));
proximity = 1.000;
save_criteria = 3;
save_criteria2 = 2;
for i = 1:SizeA
for j = 1:(i-1)
dist1a(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2 + (z(i)-z(j)).^2);
dist1a(j,i) = dist1a(i,j);
end
end
i2keep = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria);
i2keep2 = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria2);
keep_x1 = x(i2keep);
keep_y1 = y(i2keep);
keep_z1 = z(i2keep);
B = [keep_x1, keep_y1, keep_z1];
C = 1:SizeA;
index = ismember(C,i2keep2);
D = A(~index,:);
E = A(index,:);
%%
[master_index,~] = find(~index');
%%
indx = knnsearch(E,D);
F = E(indx,:);
master_index = [master_index,indx];
%%
index2 = ismember(E,F);
G = E(~index2(:,1),:);
%%
indx2 = knnsearch(G,F);
H = G(indx2,:);
idx2 = ismember(A,H);
%%
index3 = ismember(G,H);
J = G(~index3(:,1),:);
%%
indx3 = knnsearch(J,H);
K = J(indx3,:);
  3 Kommentare
Vance Blake
Vance Blake am 10 Dez. 2020
Yeah i thought about that too, but some points need to be selected more than once just not appearing in the same row. So I can't eliminate them altogether since they'll need to be reused.
Vance Blake
Vance Blake am 10 Dez. 2020
Bearbeitet: Vance Blake am 10 Dez. 2020
I have looked at my problem again and the issue is that I need points to be in the knnsearch query matrix Y and the X matrix at the same time which I why I need to be able to have the query point ignore itself as it's nearest neighbor. Is that possible ?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Vance Blake
Vance Blake am 24 Dez. 2020
dist1a = nan(numel(x));
proximity = 1.000;
save_criteria = 3;
save_criteria2 = 2;
for i = 1:SizeA
for j = 1:(i-1)
dist1a(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2 + (z(i)-z(j)).^2);
i2keep = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria);
i2keep2 = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria2);
keep_x1 =
dist1a(j,i) = dist1a(i,j);
end
end
i2keep = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria);
i2keep2 = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria2);
keep_x1 = x(i2keep);
keep_y1 = y(i2keep);
keep_z1 = z(i2keep);
B = 1:SizeA;
index = ismember(B,i2keep2);
%%
col = 1;
[master_index,~] = find(~index');
master_index_size = size(master_index,1);
indx = zeros(master_index_size,1);
for i = 1:master_index_size
while master_index(i,end) ~= 1
col = col+1;
indx = master_index(i,col-1);
C = A(indx,:);
master_index(i,col) = knnsearch(A(1:indx-1,:),C);
if master_index(i,col) == 1
break
end
end
col = 1;
end

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by