1

I have some Cluster Centers and some Data Points. I want to calculate the distances as below (norm is for Euclidean distance):

            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

How can I vectorize this snippet? Thanks

4

2 回答 2

2

这种矢量化可以非常优雅地完成(如果我可以这么说的话)使用bsxfun. 不需要任何repmats

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维度向量的顺序进行一些操作以使输出完全相同(就转置而言)。

于 2012-11-30T07:30:37.753 回答
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 数组来扩展此概念,从而消除当前的 for 循环,如下所示:

costTmp = zeros(NObjects,NClusters); lambda = zeros(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 有点像 costTmp,只是多了一个维度。我还没有提供 lambda 的代码。我也不知道问题代码中的 lambda 是什么。

于 2012-11-30T05:24:52.663 回答