Question

In matlab, I am trying to get a matrix M which is 4x50, in which each column stores the four nearest neighbors to the point. I am having trouble computing the nearest neighbor from my 50x50 distance matrix.

other info. Xmat = 3000x50 , K =4 , M is suppose to be 4x50 and D is 50x50.

Editor-S:\MATLAB\Project388.m Project388.mX 63 Write your own code here 64 65 each column stores the K nearest neighbours for

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am giving an example with sample input of less number of rows and columns to find k nearest neighbours

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); // to get row and column lengths of A

[row1,col1]=size(B); // to get row and column lengths of B

dist=zeros(row,row1); // to set the distance value to find the nearest neighbour i.e., shortest distance

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)); // the shortest distance

end

[y,index]=sort(dist(i,:)); // store that index into the matrix

nnarray(i,:)=index';

end

// the above program give results of a kth neighbour of matrix A with respect to matrix B.

the output is the nearest neighbours of matrix A

5 6 7 2 1 3 4

6 5 7 2 1 3 4

7 6 5 2 1 3 4

use this program to get nearest neighbour of any value of K for your inputs

Add a comment
Know the answer?
Add Answer to:
In matlab, I am trying to get a matrix M which is 4x50, in which each column stores the four nearest neighbors to the po...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT