function [n2, n] = dist2_radians(x, c)
%DIST2_RADIANS Calculates squared distance between two sets of points.
%
% We had to modify DIST2 a bit to cope with the circular nature of
% angular space
%
% Description
% D = DIST2_RADIANS(X, C) takes two matrices of vectors and calculates the
% squared Euclidean distance between them. Both matrices must be of
% dimension 1. If X has M rows and 1 column, and C has
% L rows and 1 column, then the result has M rows and L columns. The
% I, Jth entry is the squared distance from the Ith row of X to the
% Jth row of C.
%
% See also
% GMMACTIV, KMEANS, RBFFWD
%
% Copyright (c) Ian T Nabney (1996-2001)
siz = size(x);
if sum( siz > 1 ) > 1
error('"x" dimension must be one' );
end
x = x(:);
ndata = length( x );
siz = size(c);
if sum( siz > 1 ) > 1
error('"c" dimension must be one' );
end
c = c(:);
ncentres = length( c );
n = repmat( NaN, ndata, ncentres );
n2 = repmat( NaN, ndata, ncentres );
for ic = 1:ncentres
% Centre data around this centre
n( :, ic ) = - pi + mod( x - c( ic ) + pi, 2 * pi );
end
n2 = n .^ 2;
% Rounding errors occasionally cause negative entries in n2
if any(any(n2<0))
n2(n2<0) = 0;
end