function D = dist(A, B)
%DIST Computes Euclidean distance matrix.
%
%  Syntax
%
%    D = dist(A, B)
%
%
%  Description
%
%    DIST computes the Euclidean distance between two vectors by:
%    ||A-B|| = sqrt(||A||^2 + ||B||^2 - 2*A.B)
%
%    D = dist(A,B) takes,
%      A  - (DxM) matrix 
%      B  - (DxN) matrix
%    and returns,
%      D  - (MxN) Euclidean distances between vectors in A and B.

if nargin ~= 2
    error('Not enough input arguments.');
end

if size(A, 1) ~= size(B, 1)
    error('A and B should be of same dimensionality.');
end

aa = sum(A.*A, 1); 
bb = sum(B.*B, 1);
ab = A'*B; 
D  = sqrt(abs(repmat(aa', [1 size(bb, 2)]) + repmat(bb, [size(aa, 2) 1]) - 2*ab));
