如何在MATLAB中对代码进行矢量化

时间:2012-11-30 03:42:20

标签: matlab octave vectorization 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

如何对此代码段进行矢量化? 感谢

2 个答案:

答案 0 :(得分: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循环:

costsTmp =零(NObjects,NClusters);         lambda =零(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有点像costsTmp,只是有额外的尺寸。我没有提供lambda的代码。我也不知道lambda在问题的代码中是什么。

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