我正在尝试实现K-NN算法。如何在MATLAB中为此特定代码矢量化嵌套for循环

时间:2018-04-01 17:11:55

标签: matlab vectorization knn

我对这段代码的主要问题是效率,我想向我的代码的这一部分进行矢量化:

for x = 1:N
   for c = 1:L
      Z = in(x,1:Ks(c,1);
      Cpreds(x,c) = mode(Ctrn(Z));
   end
end

以下是我的详细实施:

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

1 个答案:

答案 0 :(得分:0)

外部循环很容易矢量化,但内部循环会改变每次迭代时传递给mode的元素数量,因此可能是不可避免的。

这是外循环的矢量化版本:

for c = 1:L
    Z = in(:,1:Ks(c,1));
    Cpreds(:,c) = mode(Ctrn(Z),2);
end