Efficiently calculating weighted distance in MATLAB

后端 未结 2 940
花落未央
花落未央 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),[]);
    

提交回复
热议问题