hii i have two matrices in A,B.I want to find k nearest neighbours of A matrix.My matlab code 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 of 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 matrix. But i want to find 5 nearest neighbours of each element in A i.e 5 nearest neighbours of A(1,1),A(1,2) etc.
How to do it? Where should I modify my code?