%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % PAIRWISE VECTOR DIFFERENCE % % (c) 2009 Joseph Rushton Wakeling % % % % Special thanks to Jaroslav Hajek for suggesting these implementations % % for efficient pairwise vector diff calculation. % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % This function calculates the pairwise differences between the columns of a % given matrix X, returning a 3D matrix D such that % % D(:,i,j) = X(:,i) - X(:,j) % function D = pairwisediff(X) if(exist('bsxfun')~=0) D = bsxfun( @minus, X, reshape(X,size(X,1),1,size(X,2)) ); else XX = reshape(X,size(X,1),1,size(X,2)); D = X(:, :, ones(1,size(X,2)) ) - XX(:, ones(1,size(X,2)), :); end end