I have 2 matrices, containing x,y data, how do i find the closest point and extract data about that point?
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Image Analyst
2013년 1월 29일
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?
채택된 답변
Shashank Prasanna
2013년 1월 29일
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
sensation
2017년 2월 7일
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!
추가 답변 (3개)
Matt J
2013년 1월 29일
This seems to come up a lot lately in conversation,
댓글 수: 2
Matt J
2013년 1월 29일
편집: Matt J
2013년 1월 29일
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
2017년 2월 7일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!