简体   繁体   中英

I am trying to implement K-NN algorithm. How can I Vectorise the nested for loops for this particular code in MATLAB

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM