2012-03-25 78 views
2

我有一個由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 

有人可以幫助我提高上述代碼的效率...或者也許建議我一個內置函數?

感謝

回答

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 
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 
+0

+1了一些證據,這太罕見這裏在SO上 – 2012-03-26 08:55:01

相關問題