我正在编写一个KNN代码,当我使用pdist或pdist2时,它是如此之慢。当我使用传统公式计算欧几里德距离时,它会更快。这是为什么?有没有办法让它更快?
%temp = [F1(i, j), F2(i, j); Xtrain(sample, 1), Xtrain(sample, 2)];
meshgrid_point = [F1(i, j), F2(i, j)];
sample_point = [Xtrain(sample, 1), Xtrain(sample, 2)];
%euclidean_distance = pdist(temp, 'euclidean');
%%euclidean_distance = pdist2(meshgrid_point, sample_point, 'euclidean');
euclidean_distance = sqrt((meshgrid_point(1) - sample_point(1))^2 + (meshgrid_point(2) - sample_point(2))^2);
答案 0 :(得分:3)
如果从matlab目录(使用搜索找到它们)复制'pdist'和'pdist2'等效mexes(“pdist2mex.mexw64”和“pdistmex.mexw64”),你可以轻松获得x8的速度提升项目目录并使用它们。 (请注意,向量输入约定是pdist.m和pdist2.m的转置版本)