My main problem with this code is efficiency,I want to vectorise this section of my code:
for x = 1:N
for c = 1:L
Z = in(x,1:Ks(c,1);
Cpreds(x,c) = mode(Ctrn(Z));
end
end
Below is my implementation in detail:
function [Cpreds] = my_knn_classify(Xtrn,Ctrn, Xtst, Ks)
% Input:
% Xtrn : M-by-D training data matrix
% Ctrn : M-by-1 label vector for Xtrn
% Xtst : N-by-D test data matrix
% Ks : L-by-1 vector of the numbers of nearest neighbours in Xtrn
% Output:
% Cpreds : N-by-L matrix of predicted labels for Xtst
[N,~] = size(Xtst);
B = Xtrn;
Ctrn = Ctrn';
[L,~] = size(Ks);
Cpreds = zeros(N, L);
DI = myfn(Xtst, B); %Vectorising euclidean distance method
[~,in] = sort(DI,2,'ascend');
for x = 1:N
for c = 1:L
Z = in(x,1:Ks(c,1));
Cpreds(x,c) = mode(Ctrn(Z));
end
end
The outer loop is straightforward to vectorise, but the inner loop changes the number of elements passed to mode
on each iteration, so is probably unavoidable.
Here is a vectorised version of the outer loop:
for c = 1:L
Z = in(:,1:Ks(c,1));
Cpreds(:,c) = mode(Ctrn(Z),2);
end