MATLAB: I have 2 matrices, containing x,y data, how do i find the closest point and extract data about that point

nearest neighbour within 2 datasets

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

Best Answer

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)