Parcourir la source

First matlab approach for linear implementation (V0)

master
Christos Choutouridis il y a 2 semaines
Parent
révision
1c60b502b3
5 fichiers modifiés avec 84 ajouts et 0 suppressions
  1. +18
    -0
      .gitignore
  2. BIN
      homework_1/Exercise 1-knn-openmp.pdf
  3. +18
    -0
      homework_1/matlab/dist2.m
  4. +24
    -0
      homework_1/matlab/knnsearch2.m
  5. +24
    -0
      homework_1/matlab/test.m

+ 18
- 0
.gitignore Voir le fichier

@@ -0,0 +1,18 @@
# project
bin/
out/
mat/
mtx/

# hpc
exclude
hpc_auth_sync.sh

# eclipse
.project
.cproject
.settings/

# matlab
*.m~


BIN
homework_1/Exercise 1-knn-openmp.pdf Voir le fichier


+ 18
- 0
homework_1/matlab/dist2.m Voir le fichier

@@ -0,0 +1,18 @@
function [D2] = dist2(X, Y)
% Calculates the squares of the distances of X and Y
%
% X: A mxd array with m d-dimentional points
% Y: A nxd array with n d-dimentional points
% d: Must be the same

[~, d1] = size(X);
[~, d2] = size(Y);
if d1 ~= d2
error('X,Y column dimensions must match');
end
%D2 = sqrt((X.^2)*ones(d,m) -2*X*Y' + ones(n,d)*(Y.^2)');
D2 = max(sum(X.^2, 2) - 2 * X*Y' + sum(Y.^2, 2)', 0);
D2 = sqrt(D2);
end


+ 24
- 0
homework_1/matlab/knnsearch2.m Voir le fichier

@@ -0,0 +1,24 @@
function [idx, dst] = knnsearch2(C, Q, k)
% C: Is a mxd matrix (Corpus)
% Q: Is a nxd matrix (Query)
% k: The number of nearest neighbors needded

% Calculate the distance matrix between C and Q
% D is an m x n matrix where each element D(i, j) is the distance
% between the i-th point in C and the j-th point in Q.
% k is the number of nearest neighbors to find.
D = dist2(C, Q);

% Find the k-nearest neighbors for each query point in Q
% [~,n] = size(D);
% for j = 1:n
% [dst(:, j), idx(:, j)] = mink(D(:, j), k);
% end
[dst, idx] = mink(D, k, 1); % mink along dimension 1 for each query point
% Transpose the output to match the knnsearch format
idx = idx'; % Make idx an n x k matrix
dst = dst'; % Make dst an n x k matrix

end


+ 24
- 0
homework_1/matlab/test.m Voir le fichier

@@ -0,0 +1,24 @@
%
%
%

C = rand(40000,4);
Q = rand(4000,4);

disp ('C-Q');
disp ('build-in')
tic; [i1, d1] = knnsearch(C, Q, 'k', 4); toc
disp ('mine')
tic; [i2, d2] = knnsearch2(C, Q, 4); toc
sum(i1-i2)
sum(d1-d2)

disp (' ');
disp (' ');
disp ('C-C');
disp ('build-in')
tic; [i1, d1] = knnsearch(C, C, 'k', 4); toc
disp ('mine')
tic; [i2, d2] = knnsearch2(C, C, 4); toc
sum(i1-i2)
sum(d1-d2)

Chargement…
Annuler
Enregistrer