spm_cross.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. function [Y] = spm_cross(X,x,varargin)
  2. % Multidimensional cross (outer) product
  3. % FORMAT [Y] = spm_cross(X,x)
  4. %
  5. % X - numeric array
  6. % x - numeric array
  7. %
  8. % Y - outer product
  9. %
  10. % See also: spm_dot
  11. %__________________________________________________________________________
  12. % Copyright (C) 2015 Wellcome Trust Centre for Neuroimaging
  13. % Karl Friston
  14. % $Id: spm_cross.m 7300 2018-04-25 21:14:07Z karl $
  15. % handle single inputs
  16. %--------------------------------------------------------------------------
  17. if nargin < 2
  18. if isnumeric(X)
  19. Y = X;
  20. else
  21. Y = spm_cross(X{:});
  22. end
  23. return
  24. end
  25. % handle cell arrays
  26. %--------------------------------------------------------------------------
  27. if iscell(X), X = spm_cross(X{:}); end
  28. if iscell(x), x = spm_cross(x{:}); end
  29. % outer product of first pair of arguments (using bsxfun)
  30. %--------------------------------------------------------------------------
  31. A = reshape(full(X),[size(X) ones(1,ndims(x))]);
  32. B = reshape(full(x),[ones(1,ndims(X)) size(x)]);
  33. Y = squeeze(bsxfun(@times,A,B));
  34. % and handle remaining arguments
  35. %--------------------------------------------------------------------------
  36. for i = 1:numel(varargin)
  37. Y = spm_cross(Y,varargin{i});
  38. end