Efficiently calculating weighted distance in MATLAB

后端 未结 2 941
花落未央
花落未央 2021-01-18 08:45

Several posts exist about efficiently calculating pairwise distances in MATLAB. These posts tend to concern quickly calculating euclidean distance between large numbers of p

相关标签:
2条回答
  • 2021-01-18 09:22

    For r = 1 ("cityblock" case), you can use bsxfun to get elementwise subtractions and then use matrix-multiplication, which must speed up things. The implementation would look something like this -

    %// Calculate absolute elementiwse subtractions
    absm = abs(bsxfun(@minus,permute(A,[1 3 2]),permute(B,[3 1 2])));
    
    %// Perform matrix multiplications with the given weights and reshape
    D = reshape(reshape(absm,[],size(A,2))*wts(:),size(A,1),[]);
    
    0 讨论(0)
  • 2021-01-18 09:43

    You can replace repmat by bsxfun. Doing so avoids explicit repetition, therefore it's more memory-efficient, and probably faster:

    function D = pairdist1(A, B, wts, distancemetric)
    
        if strcmp(distancemetric,'cityblock')
            r=1;
        elseif strcmp(distancemetric,'euclidean')
            r=2;
        else
            error('Function only accepts "cityblock" and "euclidean" distance')
        end
    
        differences  = abs(bsxfun(@minus, A, permute(B, [3 2 1]))).^r;
        differences = bsxfun(@times, differences, wts).^(1/r);
        D = permute(sum(differences,2),[1,3,2]);
    
    end
    
    0 讨论(0)
提交回复
热议问题