0001 function [groupStat,groupList,groupCnt,zScore] = calcGroupStats(data,groups,statName,groupList,randStat,nRand)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 [nItems,nSets] = size(data);
0029
0030 if (nargin < 3)
0031 statName = 'mean';
0032 end
0033 if (nargin < 4)
0034 groupList = unique(groups);
0035 end
0036 if (isempty(groupList))
0037 groupList = unique(groups);
0038 end
0039 if (nargin < 5)
0040 randStat = false;
0041 end
0042 if (nargin < 6)
0043 nRand = 1000;
0044 end
0045
0046 if iscell(groups)
0047 cellFlag = true;
0048 else
0049 cellFlag = false;
0050 end
0051
0052 for i = 1:length(groupList)
0053 if (cellFlag)
0054 selGroup = strcmp(groups,groupList{i});
0055 else
0056 selGroup = (groups == groupList(i));
0057 end
0058 selData = data(selGroup,:);
0059 groupCnt(i) = sum(selGroup);
0060 groupStat(i,:) = calcStatInternal(groupCnt(i),selData,statName,nSets);
0061 end
0062
0063 groupCnt = groupCnt';
0064
0065 if (randStat)
0066 groupCntList = unique(groupCnt);
0067
0068 zScore = zeros(length(groupList),nSets);
0069
0070 for i = 1:length(groupCntList)
0071 thisGroupCnt = groupCntList(i);
0072 selGroups = find(groupCnt == thisGroupCnt);
0073 if (thisGroupCnt > 0)
0074 for j = 1:nRand
0075 randInd = randperm(nItems);
0076 randData = data(randInd(1:thisGroupCnt),:);
0077 groupStatRand(j,:) = calcStatInternal(thisGroupCnt,randData,statName,nSets);
0078 end
0079 groupStatRandMean = nanmean(groupStatRand);
0080 groupStatRandStd = nanstd(groupStatRand);
0081 nGroups = length(selGroups);
0082 zScore(selGroups,:) = (groupStat(selGroups,:)-repmat(groupStatRandMean,nGroups,1))./repmat(groupStatRandStd,nGroups,1);
0083 end
0084 end
0085 end
0086
0087 function groupStat = calcStatInternal(groupCnt,data,statName,nSets)
0088
0089 if (groupCnt > 0)
0090 switch lower(statName)
0091 case 'mean'
0092 if (groupCnt > 1)
0093 groupStat = nanmean(data);
0094 else
0095 groupStat = data;
0096 end
0097 case 'std'
0098 if (groupCnt > 1)
0099 groupStat = nanstd(data);
0100 else
0101 groupStat = zeros(1,nSets);
0102 end
0103 case 'median'
0104 if (groupCnt > 1)
0105 groupStat = nanmedian(data);
0106 else
0107 groupStat = data;
0108 end
0109 case 'count'
0110 if (groupCnt > 1)
0111 groupStat = nansum(data);
0112 else
0113 groupStat = data;
0114 end
0115 end
0116
0117 else
0118 groupStat = ones(1,nSets)*NaN;
0119 end