根据欧几里德距离对点进行分类 - 优化代码

时间:2012-03-25 18:38:25

标签: performance matlab

我有一个由200个大小为d的载体组成的矩阵A.

我希望由4096个向量组成的矩阵B根据最近的距离规则被分类到这些点。

因此,结果应该有大小为B的行,它具有所属的id号(从1到200)。

我已经通过2 for循环编写了这段代码,需要花费大量的时间进行计算。

for i = 1:4096
        counter = 1;
        vector1 = FaceImage(i,:);
        vector2 = Centroids(1,:);
        distance = pdist( [ vector1 ; vector2] , 'euclidean' );
        for j = 2:200
              vector2 = Centroids(j,:);
              temp = pdist( [ vector1 ; vector2] , 'euclidean' );
              if temp < distance
                    distance = temp;
                    counter = j;
              end
        end
        Histogram( i ) = counter;
end

有人可以帮助我提高上述代码的效率......或者可能会建议我使用内置函数吗?

由于

2 个答案:

答案 0 :(得分:4)

您可以使用pdist2一行执行此操作:

[~, Histogram] = pdist2( Centroids, FaceImage, 'euclidian', 'Smallest', 1);

原始代码的时间安排:

FaceImage = rand(4096, 100);
Centroids = rand(200, 100);

tic
* your code *
toc

Elapsed time is 87.434877 seconds.

我的代码的时间安排:

tic
[~, Histogram_2] = pdist2( Centroids, FaceImage, 'euclidean', 'Smallest', 1);
toc

Elapsed time is 0.111736 seconds.

断言结果是相同的:

>> all(Histogram==Histogram_2)

ans =

   1

答案 1 :(得分:1)

试试这个

vector2 = Centroids(1,:);
vector = [ vector2 ; FaceImage ];
temp = pdist( vector , 'euclidean' );
answer = temp[1:4096];  % will contain first 4096 lines as distances between vector2 and rows of Face Image
Now you can find the minimum of these distances and that `row + 1` will be the vector that is closest to the point