forked from kndiaye/matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dgrouping.m
67 lines (61 loc) · 1.76 KB
/
dgrouping.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function [I,nl,F] = dgrouping(G,S)
% dgrouping() - Get indices of data elements using grouping variable in multidimension (myanova)
%
% [I,NL,F] = dgrouping(G)
%
% G is a grouping variable with as many colunms (N) as there are
% factors in the design and as many unique elements per column as
% they are levels in the corresponding factor.
% G must represent BALANCED data across levels in each factor.
% I are the indices of the data in a N-dimensional array (one
% dimension per factor)
% NL : number of levels in each factor (size of reshaped data)
% F are the factors
%
% See also: myanova
if prod(G)==max(size(G)),G=G(:);end
% number of factors
nf=size(G,2);
% total number of elements (inc. replications)
ne=size(G,1);
% output indices
I=zeros(ne,1);
% number of cells in the design
nc=1;
% if length(unique(G, 'rows'))<ne
% error('G should not contain replicates')
% end
for i=1:nf
[F{i},ig{i},g{1,i}]=unique(G(:,i));
% F: lists the levels in each factor
% ig{i}(j): indices of one the occurences of the j-th level for factor i in the array
% g{i}(k): in which level of factor i belong the k-th element in data
% number of levels in the factor
nl(i)=length(F{i});
% number of cells is increased
nc=nc*nl(i);
end
if nc==ne
% There are no replicates...
I=sub2ind(nl,g{:});
return
end
% Number of replicates
nr=0;
for i=1:nc
j=ind2sub2(nl,i);
nr=max(nr,sum(all(repmat(j,[ne,1])==G,2)));
end
nl=[nl nr];
for i=1:nf
if i==1
I=I+g{i};
else
I=I+prod(nl(1:i-1))*(g{i}-1);
end
end
% accounts for replicates in indices
for i=1:nc
I=I+(cumsum(I==i)-1).*(I==i)*nc;
end
return