spm_sdot.m 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. function [Y] = spm_sdot(X,x,k)
  2. % Sparse multidimensional dot (inner) product
  3. % FORMAT [Y] = spm_sdot(X,x,[DIM])
  4. %
  5. % X - numeric array
  6. % x - cell array of numeric vectors
  7. % DIM - dimension to omit (asumes ndims(X) = numel(x))
  8. %
  9. % Y - inner product obtained by summing the products of X and x along DIM
  10. %
  11. % If DIM is not specified the leading dimensions of X are omitted. This
  12. % routine assumes X is sparse
  13. %
  14. % See also: spm_dot, spm_cross
  15. %__________________________________________________________________________
  16. % Copyright (C) 2015 Wellcome Trust Centre for Neuroimaging
  17. % Karl Friston
  18. % $Id: spm_sdot.m 7300 2018-04-25 21:14:07Z karl $
  19. % initialise dimensions
  20. %--------------------------------------------------------------------------
  21. DIMS = size(X);
  22. NDIM = numel(DIMS);
  23. J = 1:NDIM;
  24. J(k) = [];
  25. I = cell(1,NDIM);
  26. j = find(X > exp(-16));
  27. [I{:}] = ind2sub(DIMS,j);
  28. % sum of products
  29. %--------------------------------------------------------------------------
  30. Y = zeros(numel(x{k}),1);
  31. for i = 1:numel(j)
  32. p = X(j(i));
  33. for d = J
  34. p = p*x{d}(I{d}(i));
  35. end
  36. Y(I{k}(i)) = Y(I{k}(i)) + p;
  37. end