Nearest Neighbor Matching without Replacement
11 次查看(过去 30 天)
显示 更早的评论
Hello there,
I am looking to match data in two vectors, x and y, based on shortest Euclidean distance. Each match should be unique; that is, numbers in vectors x and y cannot be matched twice. I have looked into knnsearch, but have not found anything that suggests the function works without replacement. Thank you!
回答(2 个)
neuroDuck
2022-2-25
A late response, but for anyone that might come across this in future, I think a brute force approach should work, with the following code, assuming you don't have too many comparisons to go through:
[cIdx] = unique(knnsearch(x,y));
% brute force knnsearch to do without replacements
startingK = 2;
while length(cIdx)<length(y)
[cIdx] = unique(knnsearch(x,y,'k',startingK));
startingK = startingK + 1;
end
0 个评论
Bruno Luong
2022-2-25
If you have R2019a release
x=rand(1,10)
y=rand(1,10)
C=abs(x(:)-y(:).');
M = matchpairs(C,max(C(:)));
px = M(:,1);
xm = x(px)
d = abs(xm-y)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!