forked from LIMO-EEG-Toolbox/limo_tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
limo_make_interactions.m
88 lines (81 loc) · 2.69 KB
/
limo_make_interactions.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function [tmpX interactions] = limo_make_interactions(x, nb_conditions)
% internal routine copied from limo_design_matrix to get the interaction terms
%
% FORMAT: [tmpX interactions] = limo_make_interactions(x, nb_conditions)
%
% INPUT: x the design matrix
% nb_conditions = vector with levels per factor e.g. [3 2 2]
% OUTPUT tmpX: the interaction matrix
% interactions = the vector of levels per interaction e.g. [6 6 4 12]
%
% Cyril Pernet v1 - 21-06-2013
% v2 fixed bug for designs > 3 factors - 05-07-2013
% -------------------------------------------------
% Copyright (C) LIMO Team 2015
if nb_conditions == 0
disp('number of condition = 0')
tmpX = []; interactions = [];
return
end
% get each part of x for the right factors
nb_factors = size(nb_conditions,2);
F{1} = x(:,1:nb_conditions(1)); index = nb_conditions(1)+1;
for f = 2:nb_factors
F{f} = x(:,index:(index+nb_conditions(f)-1));
index = index+nb_conditions(f);
end
% look for which factors to combine
index = 1;
for n=2:nb_factors
interaction{index} = nchoosek([1:nb_factors],n);
index = index + 1;
end
% combine those factors
tmpX = x;
index = 1;
for i=1:size(interaction,2) % for each interaction levels
for j=1:size(interaction{i},1) % for each interaction in this level
combination = interaction{i}(j,:);
if size(combination,2) == 2 % 2 factors
I = [];
a = F{combination(1)};
b = F{combination(2)};
for m=1:size(a,2)
tmp = repmat(a(:,m),1,size(b,2)).*b;
I = [I tmp];
end
I = I(:,find(sum(I))); % removes the silly zero columns
else % > 2 factors
l = 1;
while l < size(combination,2)
if l == 1
I = [];
a = F{combination(l)};
b = F{combination(l+1)};
for m=1:size(a,2)
tmp = repmat(a(:,m),1,size(b,2)).*b;
I = [I tmp];
end
I = I(:,find(sum(I)));
C{l} = I; l = l+1;
else
I = [];
a = C{l-1};
b = F{combination(l+1)};
for m=1:size(a,2)
tmp = repmat(a(:,m),1,size(b,2)).*b;
I = [I tmp];
clear tmp
end
I = I(:,find(sum(I)));
C{l} = I; l = l+1;
end
end
I = C{end};
clear C
end
interactions(index) = size(I,2);
tmpX = [tmpX I];
index = index +1;
end
end