本文介绍了执行输入参数的所有可能排列(二进制样式逻辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些代码,该代码将为函数提供未知数量的参数.想法是在可能的值范围内向函数提供最小值,中间值和最大值.

I am trying to write some code which feeds a function with an unknown number of parameters. The idea is to feed the function the minimum, middle and maximum value within the possible range of values.

例如:

  • 如果函数带有3个参数
  • 参数1可以接受的范围是0-10
  • 参数2可以接受的范围为20-40
  • 参数3可以接受的范围是6-66

myfunction(para1,para2,para3)
myfunction(min,min,min)
myfunction(min,min,mid)
myfunction(min,min,max)
myfunction(min,mid,min)
myfunction(最小,中,中)
myfunction(min,mid,max)
等等...

myfunction(para1, para2, para3)
myfunction(min,min,min)
myfunction(min,min,mid)
myfunction(min,min,max)
myfunction(min,mid,min)
myfunction(min,mid,mid)
myfunction(min,mid,max)
etc...

所以使用上面的示例:

so using our example above:

第一次循环时我需要运行
myfunction(0,20,0)
下次循环时需要运行
myfunction(0,20,36)
下次循环时需要运行
myfunction(0,20,66)
等等...

The first time it loops I need to run
myfunction(0, 20, 0)
next time it loops it needs to run
myfunction(0, 20, 36)
next time it loops it needs to run
myfunction(0, 20, 66)
etc...

对于所有可能的组合(在这种情况下,全部为27).
但是,如果参数数量更改为接受4,则需要能够容纳该参数,依此类推.我已经研究过将其作为循环或递归进行,但我认为作为循环会更容易理解,但两者都非常有用.

我不想手动执行此操作,因此将不胜感激.

For all possible combinations (in this case all 27).
However, if the number of parameters changes to accept 4, it needs to be able to accommodate that and so on. I've looked into doing it as a loop or recursively, but I thought as a loop would be easier to understand, but either would be really helpful.

I don't want to have to do this manually so any help will be much appreciated.

推荐答案

对于任意数量的参数(不固定为3),可以使用以下代码.它利用逗号-分隔列表,这是处理可变数量参数的强大工具.

For an arbitrary number of parameters (not fixed to 3), you can use the following code. It makes use of comma-separated lists, which is a powerful tool to handle a variable number of parameters.

让n为参数的数量.那么组合的数量为N = 3 ^ n.

Let n be the number of parameters. Then the number of combinations is N = 3^n.

params = {linspace(0,10,3), linspace(20,40,3), linspace(6,66,3)};
%// cell array with arbitrary number of elements. Each cell contains a 3-vector
%// which defines min, mid, max of one parameter.

n = numel(params); %// number of parameters
N = 3^n; %// number of combinations
paramCombs = cell(1,n); %// initialization for output of ndgrid
[paramCombs{end:-1:1}] = ndgrid(params{end:-1:1}); %// generate all combinations
%// in desired order. Gives n matrices, each containing values of one parameter
paramCombs = cellfun(@(c) c(:), paramCombs, 'uni', 0); %// linearize matrices
%// into n column vectors, each with N rows.
paramCombs = [paramCombs{:}]; %// concat column vectors into N x n matrix
paramCombs = mat2cell(paramCombs,ones(N,1),ones(n,1)); %// convert to
%// N x n cell array. Each row contains a combination of parameter values
result = arrayfun(@(n) myFun(paramCombs{n,:}), 1:N, 'uni', 0); %// call myFun
%// with each combination of parameter values

result变量是一个1 x N的单元格数组,其中每个单元格包含n参数组合调用myFun的结果.

The result variable is a 1 x N cell array, where each cell contains the result of calling myFun with a combination of the n parameters.

示例1 :myFun的输出仅复制输入(如 @thewaywewalk的答案). params的定义如上,所以有3个参数:

Example 1: myFun's output simply replicates the input (as in @thewaywewalk's answer). params is defined as given above, so there are 3 parameters:

>> result{1}
ans =
     0    20     6
>> result{2}
ans =
     0    20    36
>> result{3}
ans =
     0    20    66
>> result{4}
ans =
     0    30     6
>> result{5}
ans =
     0    30    36

示例2 :带有2个参数的情况:params = {linspace(0,2,3), linspace(0,10,3)}.同样,myFun只是复制输入:

Example 2: case with 2 parameters: params = {linspace(0,2,3), linspace(0,10,3)}. Again, myFun simply replicates the input:

>> result{1}
ans =
     0     0
>> result{2}
ans =
     0     5
>> result{3}
ans =
     0    10
>> result{4}
ans =
     1     0
>> result{5}
ans =
     1     5

该方法可以进一步推广为每个参数的任意(可能不同)数量的值,只需将N = 3^n;行替换为

The method can be further generalized to an arbitrary (and possibly different) number of values for each parameter, just replacing the line N = 3^n; by

N = prod(cellfun(@numel, params)); %// number of combinations

示例3 :有2个参数;第一个具有3个值,第二个具有2个值:params = {[1 2 3], [10 20]};:

Example 3: there are 2 parameters; the first with 3 values, and the second with 2: params = {[1 2 3], [10 20]};:

>> result{1}
ans =
     1    10
>> result{2}
ans =
     1    20
>> result{3}
ans =
     2    10
>> result{4}
ans =
     2    20
>> result{5}
ans =
     3    10
>> result{6}
ans =
     3    20

这篇关于执行输入参数的所有可能排列(二进制样式逻辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 08:06