remo
remo

Reputation: 898

How to vectorize the code in MATLAB

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

Upvotes: 1

Views: 180

Answers (2)

Shai
Shai

Reputation: 114816

This vectorization can be done very elegantly (if I may say so) using bsxfun. No need for any 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);

you might need to play a bit with the order of the permute dimensions vector to get the output exactly the same (in terms of transposing it).

Upvotes: 2

Spectre
Spectre

Reputation: 684

Try this:

    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

The approach is to try and make the matrices of equal dimensions. You could eliminate the present for loop also by extending this concept by making 2 3D arrays like this:

costsTmp = 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]);

Here, Norm is kinda like costsTmp, just with an extra dimensions. I havent provided the code for lambda. I dont know what lambda is in the question's code too.

Upvotes: 2

Related Questions