forked from sjgershm/mfit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial_models.m
66 lines (53 loc) · 1.61 KB
/
factorial_models.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
function opts = factorial_models(Opts)
% Create a factorial space of models.
%
% USAGE: opts = factorial_models(Opts)
%
% INPUTS:
% Opts - meta options structure, with the same fields as "opts" (see
% set_opts.m) but each field accepts a vector of parameters
%
% OUTPUTS:
% opts - [1 x M] options structure, where each structure in the array
% corresponds to one model (a particular combination of settings from Opts)
%
% Sam Gershman, Nov 2015
F = fieldnames(Opts);
for f = 1:length(F)
X{f} = Opts.(F{f});
end
g = MyCombVec(X);
for m = 1:size(g,2)
for f = 1:length(F)
opts(m).(F{f}) = g(f,m);
if iscell(opts(m).(F{f})) && length(opts(m).(F{f}))==1; opts(m).(F{f}) = opts(m).(F{f}){1}; end
end
end
end
%=========================================================
function out = MyCombVec(X)
% Generate all possible combinations of input vectors.
if nargin == 0
out = [];
else
out = X{1};
for i=2:length(X)
cur = X{i};
out = [copyb(out,size(cur,2)); copyi(cur,size(out,2))];
end
end
end
%=========================================================
function b = copyb(mat,s)
[~,mc] = size(mat);
inds = 1:mc;
inds = inds(ones(s,1),:).';
b = mat(:,inds(:));
end
%=========================================================
function b = copyi(mat,s)
[~,mc] = size(mat);
inds = 1:mc;
inds = inds(ones(s,1),:);
b = mat(:,inds(:));
end