fractal-mosaics/reference/upstream-matlab/kNearestNeighbors_no_sort.m
copilot 5bfeae238d Add Python fractal-mosaic CLI and unpack MATLAB reference
Fork of s-ben/Fractal-Mosaics for Tower mosaik inbox: GPL license,
upstream .m sources under reference/, RST placement with CBIR +
log-polar hints, edge alpha, profiles wenig/mittel/dicht, no recolor.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-24 17:03:12 +02:00

41 lines
No EOL
1.4 KiB
Matlab
Executable file

function [neighborDistances] = kNearestNeighbors_no_sort(dataMatrix, queryMatrix, k)
%--------------------------------------------------------------------------
% Program to find the k - nearest neighbors (kNN) within a set of points.
% Distance metric used: Euclidean distance
%
% Usage:
% [neighbors distances] = kNearestNeighbors(dataMatrix, queryMatrix, k);
% dataMatrix (N x D) - N vectors with dimensionality D (within which we search for the nearest neighbors)
% queryMatrix (M x D) - M query vectors with dimensionality D
% k (1 x 1) - Number of nearest neighbors desired
%
% Example:
% a = [1 1; 2 2; 3 2; 4 4; 5 6];
% b = [1 1; 2 1; 6 2];
% [neighbors distances] = kNearestNeighbors(a,b,2);
%
% Output:
% neighbors =
% 1 2
% 1 2
% 4 3
%
% distances =
% 0 1.4142
% 1.0000 1.0000
% 2.8284 3.0000
%--------------------------------------------------------------------------
% neighborIds = zeros(size(queryMatrix,1),k);
% neighborDistances = neighborIds;
neighborDistances= zeros(size(queryMatrix,1),k);
numDataVectors = size(dataMatrix,1);
numQueryVectors = size(queryMatrix,1);
for i=1:numQueryVectors,
neighborDistances = sum((repmat(queryMatrix(i,:),numDataVectors,1)-dataMatrix).^2,2);
% [sortval sortpos] = sort(dist,'ascend');
% neighborIds(i,:) = sortpos(1:k);
% neighborDistances(i,:) = sqrt(sortval(1:k));
end