I have 2 matrices, containing x,y data, how do i find the closest point and extract data about that point?
5 views (last 30 days)
Show older comments
Richard Sims
on 29 Jan 2013
Commented: sensation
on 7 Feb 2017
Hey matlabians!
A is a matrix with two columns, A= [X,Y], that give the position x and y.
B is a matrix with 3 columns,B=[X,Y,P], the position x and y, and P is simply a value assigned to that position in space.
I could use a nearest neighbour search to find the value of B closest to A, what i actually want is for every point A find the closest B and give A the value of P.
I have seen nearest neighbor analysis attempted elsewhere but that has always been for one dataset, is there a command for this? does anyone have any help, it would help me out no end. Many thanks Rich
2 Comments
Image Analyst
on 29 Jan 2013
Is B a 3D matrix, or an N rows by 3 column matrix? What does P represent? A Z value? If the Pythagorean theorem of any use to you?
Accepted Answer
Shashank Prasanna
on 29 Jan 2013
Why don't you use knnsearch in MATLAB and the indices of the point that is closest in B that in A, and use the index to extract the P value.
IDX = knnsearch(B(:,1:2),A)
B(:,IDX)
7 Comments
sensation
on 7 Feb 2017
Hi, what about finding the neareast number and its id betweeen two different size arrays?
for instance, I have an array: A: 540x1 values; and another array B, 540 X 4860 consisted of 540 blocks (each one represented by 3x3 matrix-so 9X540=4860).
What I want to do is to search through each of these 9 area block numbers, compare it with single number from B and retrieve that area.
A: 45 etc X 540
B: 5 6 9
4 15 16
21 55 2
etc X 540 blocks like this;
result: would be 55 and id=8 since the counting in my code goes like:
234
516
789 Thanks a lot for any tip!
More Answers (3)
Matt J
on 29 Jan 2013
This seems to come up a lot lately in conversation,
2 Comments
Matt J
on 29 Jan 2013
Edited: Matt J
on 29 Jan 2013
You can also use the tool to obtain the indices i, of the distance minimizing B(i). Just get the associated P(i) from that. Here's a way you can do it using my more crude distance matrix tool below
D=interdists([Ax(:),Ay(:)].', [Bx(:), By(:)].');
[~,imin]=min(D,[],2);
P_of_A=P(imin);
function Graph=interdists(A,B)
%Finds the graph of distances between point coordinates
%
% (1) Graph=interdists(A,B)
%
% in:
%
% A: matrix whose columns are coordinates of points, for example
% [[x1;y1;z1], [x2;y2;z2] ,..., [xM;yM;zM]]
% but the columns may be points in a space of any dimension, not just 3D.
%
% B: A second matrix whose columns are coordinates of points in the same
% Euclidean space. Default B=A.
%
%
% out:
%
% Graph: The MxN matrix of separation distances in l2 norm between the coordinates.
% Namely, Graph(i,j) will be the distance between A(:,i) and B(:,j).
%
%
% (2) interdists(A,'noself') is the same as interdists(A), except the output
% diagonals will be NaN instead of zero. Hence, for example, operations
% like min(interdists(A,'noself')) will ignore self-distances.
%
% See also getgraph
noself=false;
if nargin<2
B=A;
elseif ischar(B)&&strcmpi(B,'noself')
noself=true;
B=A;
end
N=size(A,1);
B=reshape(B,N,1,[]);
Graph=l2norm(bsxfun(@minus, A, B),1);
Graph=squeeze(Graph);
if noself
n=length(Graph);
Graph(linspace(1,n^2,n))=nan;
end
sensation
on 7 Feb 2017
Hi, what about finding the neareast number and its id betweeen two different size arrays?
for instance, I have an array: A: 540x1 values; and another array B, 540 X 4860 consisted of 540 blocks (each one represented by 3x3 matrix-so 9X540=4860).
What I want to do is to search through each of these 9 area block numbers, compare it with single number from B and retrieve that area.
A: 45 etc X 540
B: 5 6 9
4 15 16
21 55 2
etc X 540 blocks like this;
result: would be 55 and id=8 since the counting in my code goes like:
234
516
789
1 Comment
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!