Find one-to-one correspondence with "knnsearch" function

2 次查看(过去 30 天)
Hey everyone!
Is it possible to use the function Idx = knnsearch(X,Y) in order to get a one-to-one correspondence between X and Y? I would like to exclude all values that have been already matched with the nearest neighbor in Idx. If it's not, do you know another way to do that?
Thank you in advance.

采纳的回答

Hrishikesh Borate
Hi,
I understand that you are trying to find one to one correspondence between X and Y. One approach to find it using knnsearch is:-
load hospital;
X = [hospital.Age hospital.Weight];
tempX = X;
Y = [20 162; 30 169; 40 168; 50 170; 60 171];
for i=1:size(Y,1)
Idx = knnsearch(X,Y(i,:));
tempX(Idx,:) = inf;
id(i) = Idx;
end
Another approach is using pdist2 :-
load hospital;
X = [hospital.Age hospital.Weight];
Y = [20 162; 30 169; 40 168; 50 170; 60 171];
D = pdist2(X,Y);
[D,I] = sort(D);
temp = zeros(size(D,1),1);
id = zeros(size(D,2),1);
for i=1:size(D,2)
count = 1;
while(temp(I(count,i))~=1 || id(i)==0)
if temp(I(count,i))==0
temp(I(count,i)) = 1;
id(i) = I(count,i);
else
count = count+1;
end
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by