有条件地选择MATLAB中所有可能的参数组合 [英] Conditional selection of all possible parameter combinations in MATLAB
问题描述
除了我的参数集的所有可能组合之外,我还有一个条件参数.例如,仅当参数"corrAs"设置为目标"时,才需要包含名为"lambda"的参数.
In addition to all possible combinations of my parameter set, I also have a conditional parameter. For example, I need to include the parameter named 'lambda' only when the parameter 'corrAs' is set to 'objective'.
要做到这一点,现在我正在做以下事情
Do achieve this, right now I am doing the following
%% All posible 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'};
% If corrAs is 'objective', then also set lambda
params.lambda = {0.01, 0.1, 1, 10, 100};
%%%%%%%%%%%%% The solution posted on the link %%%%%%%%%%%
%% Get current parameter and evaluate
fields = fieldnames(params);
nFields = numel(fields);
sz = NaN(nFields, 1);
% Loop over all parameters to get sizes
for jj = 1:nFields
sz(jj) = numel( params.(fields{jj}) );
end
% Loop for every combination of parameters
idx = cell(1,nFields);
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:nFields
%%%%%%%%%%% My addition for conditional parameter %%%%%%%%%%%
% lambda is valid only when corrAs is 'objective'
if isfield(currentParam, 'corrAs') && strcmp(fields{jj}, 'lambda') && ~strcmp(currentParam.corrAs, 'objective')
continue;
end
currentParam.(fields{jj}) = params.(fields{jj}){idx{jj}};
end
%% Do something with currentParam
end
它可以工作,但是即使在corrAs不是目标"时,main for循环的迭代次数也包括lambda参数.因此,我最终使用同一个currentParam进行评估的次数比我预期的要多.
It works but, the number of iterations for the main for loop also includes the lambda parameter even when corrAs is not 'objective'. So, I end up evaluating with the same currentParam many times than I am supposed to.
我如何才能更有效地做到这一点?
How can I do it more efficiently?
推荐答案
对此进行思考的一种简单方法是将代码分解为更加基于函数的方式
An easy way to think about this is by breaking the code up to be more function-based
在下面的代码中,我仅将组合处理代码放入了函数paramProcessing
中. 该函数被调用了两次-
In the below code, I've simply put the combination processing code into a function paramProcessing
. This function is called twice -
-
仅当
params.corrAs
为'constraint'
时,将处理所有组合,而没有lambda
字段.
When
params.corrAs
is'constraint'
only, all combinations will be processed, with nolambda
field.
仅当params.corrAs
为'objective'
时,将使用附加的lambda
字段处理所有组合.
When params.corrAs
is 'objective'
only, all combinations will be processed with the additional lambda
field.
如果循环中有一个输出,则可以为paramProcessing
函数提供输出.
You can have an output for the paramProcessing
function if there is one from the looping.
这意味着您仅在进行所需的组合.从您的问题来看,每个组合似乎都是独立的,因此在单独的循环中涵盖这些组合应该无关紧要.函数用法意味着您不必在循环中添加新条件,并且每次params.corrAs
的不同可能值可确保没有重叠.
This means you're only doing the combinations you want. From your question, it seems like each combination is independent, so it should be irrelevant that you're covering the combinations in separate loops. The function usage means you don't have to have the new condition in the loop, and the distinct possible values for params.corrAs
each time ensure no overlap.
paramProcessing
函数可以是主函数文件中的本地函数,如图所示,可以是脚本中的本地函数(对于较新的MATLAB版本),也可以是路径上自己的.m文件.
The paramProcessing
function can be a local function in a main function file, as shown, local in a script (for newer MATLAB versions), or in its own .m file on your path.
代码:
function main()
%% All posible parameters, corrA is 'constraint' only.
params.corrAs = {'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'};
% First processing call, no 'lambda' field exists in 'params'
paramProcessing( params );
% Cover the cases where corrAs is 'objective', with 'lambda' field
params.corrAs = {'objective'};
params.lambda = {0.01, 0.1, 1, 10, 100};
% Second processing call, with new settings
paramsProcessing( params );
end
function paramProcessing( params )
%% Get current parameter and evaluate
fields = fieldnames(params);
nFields = numel(fields);
sz = NaN(nFields, 1);
% Loop over all parameters to get sizes
for jj = 1:nFields
sz(jj) = numel( params.(fields{jj}) );
end
% Loop for every combination of parameters
idx = cell(1,nFields);
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:nFields
currentParam.(fields{jj}) = params.(fields{jj}){idx{jj}};
end
%% Do something with currentParam
end
end
这篇关于有条件地选择MATLAB中所有可能的参数组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!