min_kernel.m 1011 B

123456789101112131415161718192021222324252627282930313233
  1. function C = min_kernel(a, b)
  2. % borrowed from gpml-toolbox of Rasmussen and Nikkisch
  3. % see http://www.gaussianprocess.org/gpml/code/matlab/doc/
  4. if nargin<1 || nargin>2 || nargout>1, error('Wrong number of arguments.'); end
  5. bsx = exist('bsxfun','builtin'); % since Matlab R2007a 7.4.0 and Octave 3.0
  6. if ~bsx, bsx = exist('bsxfun'); end % bsxfun is not yes "builtin" in Octave
  7. [D, n] = size(a);
  8. if nargin==1 % subtract mean
  9. b = a; m = n;
  10. else
  11. [d, m] = size(b);
  12. if d ~= D, error('Error: column lengths must agree.'); end
  13. end
  14. if bsx % compute squared distances
  15. C = zeros(n,m);
  16. % This code is working but still inefficient :(
  17. if ( n > m )
  18. for i=1:m
  19. C(:,i)=sum(bsxfun(@min,a,b(:,i)),1)';
  20. end
  21. else
  22. for j=1:n
  23. C(j,:)=sum(bsxfun(@min,a(:,j),b),1);
  24. end
  25. end
  26. else
  27. error('not yet implemented...');
  28. end
  29. C = max(C,0); % numerical noise can cause C to negative i.e. C > -1e-14