spm_meanby.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. function [M,Mi,i] = spm_meanby(Y,I)
  2. % Means of data in columns by group
  3. % FORMAT [M,Mi,i] = spm_meanby(Y,I)
  4. % Y - Data matrix, data in columns. (Row vector Y also accepted.)
  5. % I - Column of indicator vectors, indicating group membership of rows of Y
  6. % - Multi-column I are treated as multiple factors to be interacted,
  7. % and means are computed within each unique combination of the factor levels
  8. % M - Matrix of same size as Y, with observations replaced by the
  9. % appropriate group mean
  10. % Mi - Mean for observations in each group, one column for each column of Y,
  11. % one row for each group (or unique factor level combination)
  12. % i - Group indicator values corresponding to rows of Mi
  13. %_______________________________________________________________________
  14. %
  15. % spm_meanby computes means for grouped data presented as columns of
  16. % data with a vector of group indicators
  17. %_______________________________________________________________________
  18. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  19. % Andrew Holmes
  20. % $Id: spm_meanby.m 1143 2008-02-07 19:33:33Z spm $
  21. %-Check arguments
  22. %=======================================================================
  23. if nargin<0, error('insufficient arguments'), end
  24. if size(Y,1)==1, bT=1; Y=Y'; else, bT=0; end
  25. if nargin<2, I=ones(size(Y,1),1); end
  26. if size(I,1)~=size(Y,1), I=I'; end
  27. if size(I,1)~=size(Y,1), error('row dimension mismatch between inputs'), end
  28. %-Computation - using spm_DesMtx
  29. %=======================================================================
  30. [X,null,i,idx,jdx] = spm_DesMtx(I);
  31. i = i';
  32. Mi = (X'*Y)./repmat(sum(X,1)',1,size(Y,2));
  33. M = Mi(jdx,:);
  34. if bT, M=M'; end