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)
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
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?
Richard Sims
Richard Sims on 29 Jan 2013
Edited: Richard Sims on 29 Jan 2013
A is a matrix with two columns, that give the position x and y.
B is a matrix with 3 columns, 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 apologize if my question wasn't clear before

Sign in to comment.

Accepted Answer

Shashank Prasanna
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
Richard Sims
Richard Sims on 2 Feb 2013
Edited: Richard Sims on 2 Feb 2013
do you perhaps know anyway of altering this code to find the nearest neighbor if A and B were latitude and longitudes?
The stdist command works using latitude and longitude distances but then i wouldn't be able to index the data any longer like in your example above. The knnsearch help box says it accepts custom distance functions but i don't seem to be able to find one. Sorry to keep bothering you!
sensation
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!

Sign in to comment.

More Answers (3)

Sean de Wolski
Sean de Wolski on 29 Jan 2013
How about using dsearchn() to find the nearest neighbors?

Matt J
Matt J on 29 Jan 2013
  2 Comments
Richard Sims
Richard Sims on 29 Jan 2013
If the two matrices were A= [X,Y], B=[X,Y] then i think this solution would be perfect for determining the distances.
However i want to find the value P from matrix B and assign it to A and i don't believe this works for such a problem A= [X,Y], B=[X,Y,P]
Matt J
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

Sign in to comment.


sensation
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

Community Treasure Hunt

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

Start Hunting!