spm_vb_edgeweights.m 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. function [edges,weights] = spm_vb_edgeweights(vxyz,img)
  2. % Compute edge set and edge weights of a graph
  3. % FORMAT [edges,weights]= spm_vb_edgeweights(vxyz,img)
  4. %
  5. % vxyz list of neighbouring voxels (see spm_vb_neighbors)
  6. % img image defined on the node set, e.g. wk_ols. The edge weights
  7. % are uniform if this is not given, otherwise they are a function
  8. % of the distance in physical space and that between the image
  9. % at neighoring nodes
  10. % edges [Ne x 2] list of neighboring voxel indices
  11. % weights [Ne x 1] list of edge weights
  12. % Ne number of edges (cardinality of edges set)
  13. % N number of nodes (cardinality of node set)
  14. %__________________________________________________________________________
  15. % Copyright (C) 2008-2014 Wellcome Trust Centre for Neuroimaging
  16. % Lee Harrison
  17. % $Id: spm_vb_edgeweights.m 6079 2014-06-30 18:25:37Z spm $
  18. N = size(vxyz,1);
  19. [r,c,v] = find(vxyz');
  20. edges = [c,v];
  21. % undirected graph, so only need store upper [lower] triangle
  22. i = find(edges(:,2) > edges(:,1));
  23. edges = edges(i,:);
  24. if nargin < 2,
  25. weights = ones(size(edges,1),1);
  26. return
  27. else
  28. ka = 16;
  29. M = mean(img,2)*ones(1,N);
  30. C = (1/N)*(img-M)*(img-M)';
  31. Hf = inv(C);
  32. A = spm_vb_incidence(edges,N);
  33. dB = img*A'; % spatial gradients of ols estimates
  34. dg2 = sum((dB'*Hf).*dB',2); % squared norm of spatial gradient of regressors
  35. ds2 = 1 + dg2; % squared distance in space is 1 as use only nearest neighbors
  36. weights = exp(-ds2/ka); % edge weights
  37. end