许多参数的所有可能组合MATLAB [英] All possible combinations of many parameters MATLAB
问题描述
我有一个参数列表,我需要在该列表上评估我的方法.现在,我正在这样做
I have a list of parameters and I need to evaluate my method over this list. Right now, I am doing it this way
% Parameters
params.corrAs = {'objective', 'constraint'};
params.size = {'small', 'medium', 'large'};
params.density = {'uniform', 'non-uniform'};
params.k = {3,4,5,6};
params.constraintP = {'identity', 'none'};
params.Npoints_perJ = {2, 3};
params.sampling = {'hks', 'fps'};
% Select the current parameter
for corrAs_iter = params.corrAs
for size_iter = params.size
for density_iter = params.density
for k_iter = params.k
for constraintP_iter = params.constraintP
for Npoints_perJ_iter = params.Npoints_perJ
for sampling_iter = params.sampling
currentParam.corrAs = corrAs_iter;
currentParam.size = size_iter;
currentParam.density = density_iter;
currentParam.k = k_iter;
currentParam.constraintP = constraintP_iter;
currentParam.Npoints_perJ = Npoints_perJ_iter;
currentParam.sampling = sampling_iter;
evaluateMethod(currentParam);
end
end
end
end
end
end
end
我知道它看起来很丑陋,如果我想添加新类型的参数,则必须编写另一个for循环.有什么办法,我可以向量化吗?或者也许使用2个for循环而不是那么多.
I know it looks ugly and if I want to add a new type of parameter, I have to write another for loop. Is there any way, I can vectorize this? Or maybe use 2 for loops instead of so many.
我尝试了以下操作,但是并不能满足我的需要.
I tried the following but, it doesn't result in what I need.
for i = 1:numel(fields)
% if isempty(params.(fields{i}))
param.(fields{i}) = params.(fields{i})(1);
params.(fields{i})(1) = [];
end
推荐答案
What you need is all combinations of your input parameters. Unfortunately, as you add more parameters the storage requirements will grow quickly (and you'll have to use a large indexing matrix).
相反,这是一个使用(从未创建的)n1*n2*...*nm
矩阵的线性索引的想法,其中ni
是m
字段的每个字段中的元素数.
Instead, here is an idea which uses linear indicies of a (never created) n1*n2*...*nm
matrix, where ni
is the number of elements in each field, for m
fields.
它足够灵活,可以应付添加到params
的任何数量的字段.未经性能测试,尽管与任何所有组合"操作一样,您应警惕在params
中添加更多字段时计算时间的非线性增加,请注意prod(sz)
!
It is flexible enough to cope with any amount of fields being added to params
. Not performance tested, although as with any "all combinations" operation you should be wary of the non-linear increase in computation time as you add more fields to params
, note prod(sz)
!
我显示的代码很快,但是性能将完全取决于您在循环中执行的操作.
The code I've shown is fast, but the performance will depend entirely on which operations you do in the loop.
% Add parameters here
params.corrAs = {'objective', 'constraint'};
params.size = {'small', 'medium', 'large'};
params.density = {'uniform', 'non-uniform'};
% Setup
f = fieldnames( params );
nf = numel(f);
sz = NaN( nf, 1 );
% Loop over all parameters to get sizes
for jj = 1:nf
sz(jj) = numel( params.(f{jj}) );
end
% Loop for every combination of parameters
idx = cell(1,nf);
for ii = 1:prod(sz)
% Use ind2sub to switch from a linear index to the combination set
[idx{:}] = ind2sub( sz, ii );
% Create currentParam from the combination indices
currentParam = struct();
for jj = 1:nf
currentParam.(f{jj}) = params.(f{jj}){idx{jj}};
end
% Do something with currentParam here
% ...
end
除:
- 我正在使用动态字段名称引用为字段建立索引
- 我要将将多个输出传递到单元格中数组中的数组,因此当
ind2sub
每个维度(或本用例中的字段)有一个输出时,您可以处理可变数量的字段名称.
- I'm using dynamic field name references for indexing the fields
- I'm passing multiple outputs into a cell array from
ind2sub
, so you can handle a variable number of field names whenind2sub
has one output for each dimension (or field in this use-case).
这篇关于许多参数的所有可能组合MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!