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.
0 个评论
采纳的回答
Hrishikesh Borate
2021-2-4
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
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 Center 和 File Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!