【问题标题】:Conditional selection of all possible parameter combinations in MATLAB在 MATLAB 中条件选择所有可能的参数组合
【发布时间】:2019-06-03 12:20:58
【问题描述】:

这是All possible combinations of many parameters MATLAB问题的后续

除了我的参数集的所有可能组合之外,我还有一个条件参数。例如,只有当参数 'corrAs' 设置为 'objective' 时,我才需要包含名为 'lambda' 的参数。

做到这一点,现在我正在做以下事情

%% 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 不是“客观的”,主 for 循环的迭代次数也包括 lambda 参数。因此,我最终使用相同的 currentParam 进行了多次评估。

我怎样才能更有效地做到这一点?

【问题讨论】:

    标签: matlab struct combinations


    【解决方案1】:

    考虑这一点的一种简单方法是将代码分解为更基于函数的代码

    在下面的代码中,我只是简单地将组合处理代码放入了一个函数paramProcessing这个函数被调用了两次 -

    1. params.corrAs 仅是'constraint' 时,将处理所有组合,没有lambda 字段。

    2. params.corrAs 仅是'objective' 时,所有组合都将使用附加的lambda 字段进行处理。

    如果循环中有一个,您可以为paramProcessing 函数提供一个输出。

    这意味着你只是在做你想要的组合。从您的问题来看,似乎每个组合都是独立的,因此您在单独的循环中覆盖组合应该是无关紧要的。函数用法意味着您不必在循环中添加新条件,并且每次params.corrAs 的不同可能值确保没有重叠。

    paramProcessing 函数可以是主函数文件中的本地函数(如图所示),也可以是脚本中的本地函数(对于较新的 MATLAB 版本),也可以是路径上它自己的 .m 文件中的本地函数。

    代码:

    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    
    

    【讨论】:

    • 是的。但是,这需要人工干预。有什么办法让它自动化吗?
    • 我不明白这比最低限度的人为干预多吗?您总是需要指定存在哪些param 条件(/哪些组合是例外),这只是一种分离这些条件以减少不必要循环的方法。
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多