find k nearest neighbours for each element in a matrix

17 views (last 30 days)
hii I have two matrices in A,B. My matlab code for finding k nearest neighbours of A wrt B is:
A=[1 2 1;3 4 1;5 6 1;];
B=[11 12 2;13 4 2;15 16 2;17 18 2;1 2 2;3 4 2;5 6 2;];
[row,col]=size(A);
[row1,col1]=size(B);
dist=zeros(row,row1);
nnarray = zeros(row,row1);
k=5;
nnarray1 = zeros(row,k);
for i=1:row
for j=1:row1
dist(i,j)=sqrt(sum((A(i,:)-B(j,:)).^2));
end
[y,index]=sort(dist(i,:));
nnarray(i,:)=index';
end
The ouptut matrix for nnarray is:
//nearest neighbours for A matrix
5 6 7 2 1 3 4
6 5 7 2 1 3 4
7 6 5 2 1 3 4
Here the output i got only one NEAREST NEIGHBOUR for each element in A matix wrt B.
But I want to find 5 nearest neighbours for each element in A matrix.
Eg : I want to find 5 nearest neighbours for A(1,2).
How to do it.? and Where should i modify my code
  7 Comments
Roger Stafford
Roger Stafford on 8 Sep 2013
I think all of us are having the same problem understanding what you are asking. The code you exhibited considered the Euclidean distance between the rows of A and the rows of B where the distance is the square root of the sum of the squares of the differences of the elements of the three columns of A and B respectively. In other words the distance between A(2,:) and B(5,:) for example would be
sqrt((A(2,1)-B(5,1)^2+(A(2,2)-B(5,2)^2+(A(2,3)-B(5,3)^2)
and in your result you correctly found the index 5 as pointing to the second closest row of B to the second row of A. With this meaning of distance and neighbor you have already obtained your answer, namely, the first five columns of the result you gave.
If the above is what you mean by "distance", then there is no sense asking about the nearest neighbors of a single element such as A(1,2). You have to decide what kind of objects you are finding the distance between in determining what you mean by "neighbors".
indian
indian on 8 Sep 2013
Please understand my question and leave my code.My problem is I want to find k nearest neighbours of each element in a matrix based on other matrix B I want 5 nearest neighbours of A(1,1)... basing on whole matrix B
In my answer I am finding out between rows but I want for each element in a matrix

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 7 Sep 2013
Not hard at all if you're clever, and use conv2 and sort. Check out this code:
m = magic(10) % Sample data
% Get the absolute value of hte differences
% in each of the 8 neighbor directions.
nn1 = abs(conv2(m, [-1,0,0;0,1,0;0,0,0], 'valid'))
nn2 = abs(conv2(m, [0,-1,0;0,1,0;0,0,0], 'valid'))
nn3 = abs(conv2(m, [0,0,-1;0,1,0;0,0,0], 'valid'))
nn4 = abs(conv2(m, [0,0,0;-1,1,0;0,0,0], 'valid'))
nn5 = abs(conv2(m, [0,0,0;0,1,-1;0,0,0], 'valid'))
nn6 = abs(conv2(m, [0,0,0;0,1,0;-1,0,0], 'valid'))
nn7 = abs(conv2(m, [0,0,0;0,1,0;0,-1,0], 'valid'))
nn8 = abs(conv2(m, [0,0,0;0,1,0;0,0,-1], 'valid'))
% Stack these as planes (slices) in a 3D matrix.
array3D = cat(3, nn1, nn2, nn3, nn4, nn5, nn6, nn7, nn8);
% Now sort along the z direction.
sorted3D = sort(array3D, 3);
% Let's see them - print them out just for fun.
for z = 1 : 8
array2D = sorted3D(:,:,z)
end
% Only the first 5 were wanted, so take those:
finalOoutput = sorted3D(:,:,1:5)
msgbox('Done with demo! Check the command window.');
  1 Comment
Image Analyst
Image Analyst on 8 Sep 2013
Nevermind - this code produces the 5 closest neighbors to an element in the same matrix, not between different matrices. Why do you want that anyway?

Sign in to comment.


Shashank Prasanna
Shashank Prasanna on 8 Sep 2013
Eg : I want to find 5 nearest neighbours for A(1,2).
That does not make much sense. You are finding neighbors of A(i,:), i.e. you are finding neighbors for each row of A which is treated as a vector with each row of B(j,:).
A=[1 2 1;3 4 1;5 6 1;];
B=[11 12 2;13 4 2;15 16 2;17 18 2;1 2 2;3 4 2;5 6 2;];
for i = 1:length(A)
D = sum((repmat(A(i,:),length(B),1) - B).^2,2);
[sortD idxD] = sort(D);
fiveNND{i,:} = idxD(1:5)';
end
fiveNND variable has a cell array of the index in A for the 5 nearest neighbors in B. It looks similar to what you have:
fiveNND =
[1x5 double]
[1x5 double]
[1x5 double]
Note these are indices of B and here is how you will use them:
>> B(fiveNND{1},:) % First 5 neighbors for A(1,:)
>> B(fiveNND{2},:) % First 5 neighbors for A(2,:)
>> B(fiveNND{3},:) % First 5 neighbors for A(3,:)
Hope that makes sense. In any case I would recommend the use of PDIST2 or KNNSEARCH
  8 Comments
Shashank Prasanna
Shashank Prasanna on 8 Sep 2013
Edited: Shashank Prasanna on 8 Sep 2013
If what you say is correct - rows are instances and columns are features, then go back to my first post. Neighbors are calculated in the feature space which is all the columns for each instance:
A(i,:) to B(j,:)
If you saw something in a research paper that is different, then some references would be great.
Take a look at some examples in the doc for neighbor searches:

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!