2012-11-30 4 views
1

일부 클러스터 센터와 일부 데이터 포인트가 있습니다. 나는 (표준은 유클리드 거리입니다) 아래로 거리를 계산하려면 : MATLAB에서 코드를 벡터화하는 방법

  costsTmp = zeros(NObjects,NClusters); 
      lambda = zeros(NObjects,NClusters); 
      for clustclust = 1:NClusters 
       for objobj = 1:NObjects 
        costsTmp(objobj,clustclust) = norm(curCenters(clustclust,:)-curPartData(objobj,:),'fro'); 
        lambda(objobj,clustclust) = (costsTmp(objobj,clustclust) - log(si1(clustclust,objobj)))/log(si2(objobj,clustclust)); 
       end 
      end 

가 어떻게이 조각을 벡터화 할 수

? 감사

답변

2

이 시도 :

Difference = zeros(NObjects,NClusters); 
    costsTmp = zeros(NObjects,NClusters); 
    lambda = zeros(NObjects,NClusters); 
    for clustclust = 1:NClusters 
    repeated_curCenter = repmat(curCenter(clustclust,:), NObjects, 1); 
    % ^^ This creates a repeated matrix of 1 cluster center but with NObject 
    % rows. Now, dimensions of repeated_curCenter equals that of curPartData 

    Difference(:,clustclust) = repeated_curCenter - curPartData; 
    costsTmp(:,clustclust) = sqrt(sum(abs(costsTmp(:,clustclust)).^2, 1)); %Euclidean norm 
    end 

접근을 시도하고 동일한 크기의 행렬을 만드는 것입니다. 다음과 같이 2 개의 3D 배열을 만들어이 개념을 확장하여 present for 루프를 제거 할 수도 있습니다.

costsTmp = 0 (NObjects, NClusters); 람다 = 0 (NObjects, NClusters);

%Assume that number of dimensions for data = n 
    %curCenter's dimensions = NClusters x n 
    repeated_curCenter = repmat(curCenter, 1, 1, NObjects); 
    %repeated_curCenter's dimensions = NClusters x n x NObjects 

    %curPartData's dimensions = NObject x n 
    repeated_curPartData = repmat(curPartData, 1, 1, NClusters); 
    %repeated_curPartData's dimensions = NObjects x n x NClusters 

    %Alligning the matrices along similar dimensions. After this, both matrices 
    %have dimensions of NObjects x n x NClusters 
    new_repeated_curCenter = permute(repeated_curCenter, [3, 2, 1]); 

    Difference = new_repeated_curCenter - repeated_curPartData; 

    Norm = sqrt(sum(abs(Difference)).^2, 2); %sums along the 2nd dimensions i.e. n 
    %Norm's dimensions are now NObjects x 1 x NClusters. 

    Norm = permute(Norm, [1, 3, 2]); 

여기에서 Norm은 추가 치수만으로 비용 Tmp와 비슷합니다. 나는 lambda에 대한 코드를 제공하지 않았다. 나는 람다가 질문의 코드에도 무엇인지 모른다.

2

이 벡터화는 bsxfun을 사용하여 매우 우아하게 처리 할 수 ​​있습니다. 어떤 repmat

costsTemp = bsxfun(@minus, permute(curCenters, [1 3 2]), ... 
          permute(curPartData, [3 1 2])); 
% I am not sure why you use Frobenius norm, this is the same as Euclidean norm for vector 
costsTemp = sqrt(sum(costsTemp.^2, 3)); % now we have the norms 
lambda = costsTmp -reallog(si1)./reallog(si2); 

당신은 (그것을 전치의 관점에서) 정확히 같은 출력을 얻을 수있는 permute 차원 벡터의 순서로 비트를 연주해야 할 수도 있습니다에 대한 필요가 없습니다.

+0

[1 2 3] 또는 [3 1 2]의 의미는 무엇입니까? 내 데이터 세트 도메인이 2D 또는 3D 공간에 없습니다. 내가 테스트했듯이 정규 표준은 ('이리저리'없이) 항상 필요한 정확한 값을 반환하지 않습니다! – remo

+0

@remo 여러분의 코드에서 'curCenters'는 D 차원의 CxD : C 중심 크기의 2D 배열 인 것처럼 보일 것입니다. 'curPartData'는 D 차원의 PxD : P 점 크기의 2D 배열로 보입니다. 위의 코드는 모든 CxP 차이를 계산하기 위해 두 개의 2D 행렬을 싱글 튼 차원의 Cx1xD 및 1xPxD 3D 행렬로 바꿉니다. 'bsxfun'은이 싱글 톤 차원을 이용하고 차이점의 CxPxD 배열을 계산합니다. 이 3D 결과는'costsTemp'에 저장됩니다. 이를 제곱하고 3 차원을 합하면 원하는 결과를 얻습니다. – Shai

+0

@remo이 해결책이 당신에게 좋은가요? 당신이 그것을 받아들이지 않는 이유가 있습니까? – Shai

관련 문제