将参数(即笛卡尔乘积)置换成多维阵列

时间:2013-11-15 00:31:00

标签: matlab combinatorics cartesian-product

我有兴趣计算一组参数值的排列函数。我希望将它保持为N维度的通用,但是让我从3维开始编写它。使用meshgrid生成排列很容易,但我无法弄清楚如何将生成的数组重新整形回多维数?这是一个起点:

%These are the 3 variations of parameters, with some values.  
params1 = [100, 200, 300];%Picking these so it is easy to correlate to the function
params2 = [10, 20];
params3 = [1, 2];

%This generates parameter_values as the cartesian productpermutations.
[vec1, vec2, vec3] = meshgrid(params1, params2, params3);
parameter_values = [vec1(:) vec2(:) vec3(:)];

%Calculates functions on the set of parameters.
%Would have a fancier function, of course, this just makes it easy to see the results.
raw_vals = parameter_values(:,1) + parameter_values(:,2) + parameter_values(:,3); 

%Rearrange into a multiarray to access by parameter indices.
f_vals = reshape(raw_vals, [length(params1), length(params2), length(params3)]) %WRONG? 

%THE FOLLOWING FAIL BUT WOULD BE EXPECTED WITH THESE PARAMETERS AND THE FUNCTION.
assert(f_vals(2,1,1) == 211)
assert(f_vals(3,2,2) == 322)

1 个答案:

答案 0 :(得分:4)

在这种情况下,您需要ndgrid而不是meshgrid

meshgrid的语法为[X,Y] = meshgrid(xgv,ygv),导致Y(:)变化最快而非X(:)。有关详细信息,请参阅Gridded Data Representation。换句话说,你得到了

>> [vec1, vec2, vec3] = meshgrid(params1, params2, params3)
vec1(:,:,1) =
   100   200   300
   100   200   300
vec1(:,:,2) =
   100   200   300
   100   200   300
vec2(:,:,1) =
    10    10    10
    20    20    20
vec2(:,:,2) =
    10    10    10
    20    20    20
...

但是你想得到:

>> [vec1, vec2, vec3] = ndgrid(params1, params2, params3)
vec1(:,:,1) =
   100   100
   200   200
   300   300
vec1(:,:,2) =
   100   100
   200   200
   300   300
vec2(:,:,1) =
    10    20
    10    20
    10    20
vec2(:,:,2) =
    10    20
    10    20
    10    20
...

如果您切换到ndgrid,则会按预期获得f_vals(2,1,1) == 211

可以按照以下方式推广到N维:

    params = {[100, 200, 300],[10, 20],[1, 2]};
    vecs = cell(numel(params),1);
    [vecs{:}] = ndgrid(params{:});
    parameter_values = reshape(cat(numel(vecs)+1,vecs{:}),[],numel(vecs));
    raw_vals = sum(parameter_values,2);
    f_vals = reshape(raw_vals,cellfun(@numel,params))