 <p>If you want to compute the <a href="http://en.wikipedia.org/wiki/Euclidean_distance" rel="nofollow">Euclidean distance</a> between vectors <code>a</code> and <code>b</code>, just use <a href="http://en.wikipedia.org/wiki/Pythagorean_theorem" rel="nofollow">Pythagoras</a>. In Matlab:</p> <pre><code>dist = sqrt(sum((a-b).^2)); </code></pre> <p>However, you might want to use <a href="http://www.mathworks.nl/help/toolbox/stats/pdist.html" rel="nofollow"><code>pdist</code></a> to compute it for all combinations of vectors in your matrix at once.</p> <pre><code>dist = squareform(pdist(myVectors, 'euclidean')); </code></pre> <p>I'm interpreting columns as instances to classify and rows as potential neighbors. This is arbitrary though and you could switch them around.</p> <p>If have a separate test set, you can compute the distance to the instances in the training set with <code>pdist2</code>:</p> <pre><code>dist = pdist2(trainingSet, testSet, 'euclidean') </code></pre> <p>You can use this distance matrix to knn-classify your vectors as follows. I'll generate some random data to serve as example, which will result in low (around chance level) accuracy. But of course you should plug in your actual data and results will probably be better.</p> <pre><code>m = rand(nrOfVectors,nrOfFeatures); % random example data classes = randi(nrOfClasses, 1, nrOfVectors); % random true classes k = 3; % number of neighbors to consider, 3 is a common value d = squareform(pdist(m, 'euclidean')); % distance matrix [neighborvals, neighborindex] = sort(d,1); % get sorted distances </code></pre> <p>Take a look at the <code>neighborvals</code> and <code>neighborindex</code> matrices and see if they make sense to you. The first is a sorted version of the earlier <code>d</code> matrix, and the latter gives the corresponding instance numbers. Note that the self-distances (on the diagonal in <code>d</code>) have floated to the top. We're not interested in this (always zero), so we'll skip the top row in the next step.</p> <pre><code>assignedClasses = mode(neighborclasses(2:1+k,:),1); </code></pre> <p>So we assign the most common class among the k nearest neighbors!</p> <p>You can compare the assigned classes with the actual classes to get an accuracy score:</p> <pre><code>accuracy = 100 * sum(classes == assignedClasses)/length(classes); fprintf('KNN Classifier Accuracy: %.2f%%\n', 100*accuracy) </code></pre> <p>Or make a confusion matrix to see the distribution of classifications:</p> <pre><code>confusionmat(classes, assignedClasses) </code></pre>