function K = rbf4nn_mat( X, y, cov_inv )
%function K = rbf4nn_mat( X, y, cov_inv )
%
% Computes an rbf kernel matrix of X with Y, using a diagonal
% covariance matrix specified in sigsq.
%
%INPUTS
% X = a matrix containing all samples as columns.
% y = another matrix containing samples as columns.
% cov_inv = inverse of covariance matrix
%
%NOTE: does not include the normalization factor!
%
%OUTPUTS
% K_ij = exp(-0.5*(X_i' * cov_inv * Y_j))
%
% Adapted by D. Wingate from code written by Tijl De Bie.
xcnt = size( x, 2 );
ycnt = size( y, 2 );
sx = cov_inv * x;
sy = cov_inv * y;
xs = sum( x .* sx, 1 ); % x'*cov_inv*x; row vector
ys = sum( y .* sy, 1 ); % y'*cov_inv*y; row vector
mat = -0.5 * ( repmat( xs', 1, ycnt ) + repmat( ys, xcnt, 1 ) ) + sx'*y;
K = exp( mat );