spm_dem2dcm.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. function [DCM] = spm_dem2dcm(DEM,DCM)
  2. % reorganisation of posteriors and priors into DCM format
  3. % FORMAT [DCM] = spm_dem2dcm(DEM)
  4. % FORMAT [DEM] = spm_dem2dcm(DEM,DCM)
  5. %
  6. % DEM - structure array (hierarchicial model)
  7. % DCM - structure array (flat model)
  8. %
  9. % -------------------------------------------------------------------------
  10. % DCM.M.pE - prior expectation of parameters
  11. % DCM.M.pC - prior covariances of parameters
  12. % DCM.Ep - posterior expectations
  13. % DCM.Cp - posterior covariance
  14. % DCM.F - free energy
  15. %
  16. % For hierarchical models (DEM.M) the first level with non-zero prorior
  17. % varaince on the paramters will be extracted.
  18. %__________________________________________________________________________
  19. % Copyright (C) 2005 Wellcome Trust Centre for Neuroimaging
  20. % Karl Friston
  21. % $Id: spm_dem2dcm.m 6508 2015-07-25 15:23:25Z karl $
  22. % check for arrays
  23. %--------------------------------------------------------------------------
  24. if iscell(DEM)
  25. for i = 1:size(DEM,1)
  26. for j = 1:size(DEM,2)
  27. if nargin < 2
  28. DCM{i,j} = spm_dem2dcm(DEM{i,j});
  29. else
  30. DCM{i,j} = spm_dem2dcm(DEM{i,j},DCM{i,j});
  31. end
  32. end
  33. end
  34. return
  35. end
  36. % get level
  37. %--------------------------------------------------------------------------
  38. for j = 1:length(DEM.M)
  39. if any(any(DEM.M(j).pC)), break, end
  40. end
  41. % get indices for covariance
  42. %--------------------------------------------------------------------------
  43. k = spm_length(DEM.qP.P(j));
  44. k = spm_length(DEM.qP.P(1:(j - 1))) + (1:k);
  45. % re-organise
  46. %--------------------------------------------------------------------------
  47. if nargin < 2
  48. DCM.M.pE = DEM.M(j).pE;
  49. DCM.M.pC = DEM.M(j).pC;
  50. DCM.Ep = DEM.qP.P{j};
  51. DCM.Cp = DEM.qP.C(k,k);
  52. DCM.F = DEM.F(end);
  53. else
  54. DEM.M(j).pE = spm_unvec(DCM.M.pE,DEM.M(j).pE);
  55. DEM.M(j).pC = DCM.M.pC;
  56. DEM.qP.P{j} = spm_unvec(DCM.Ep,DEM.qP.P{j});
  57. DEM.qP.C(k,k) = DCM.Cp;
  58. DEM.F = DCM.F;
  59. % output argument
  60. %----------------------------------------------------------------------
  61. DCM = DEM;
  62. end