许多参数的所有可能组合MATLAB [英] All possible combinations of many parameters MATLAB

查看:258
本文介绍了许多参数的所有可能组合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矩阵的线性索引的想法,其中nim字段的每个字段中的元素数.

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

除:

  • 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 when ind2sub has one output for each dimension (or field in this use-case).

这篇关于许多参数的所有可能组合MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆