1 All Possible Combinations of Multiple Arrays

Go to the MLX, M, PDF, or HTML version of this file. Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).

1.1 Given Several Arrays of Possibly different Length in Container, all Possible combinations

param_tstar_map = containers.Map('KeyType','char', 'ValueType','any');
param_tstar_map('a') = linspace(1, 5, 5);
param_tstar_map('b') = linspace(0.87, 0.97, 6);
param_tstar_map('c') = linspace(0, 0.5, 10);

cl_st_param_keys = {'a','c'};
cl_ar_param_subset_values = values(param_tstar_map, {'a','c'});

cl_mt_all = cl_ar_param_subset_values;
[cl_mt_all{:}] = ndgrid(cl_ar_param_subset_values{:});
mt_param_vals_combi = cell2mat(cellfun(@(m) m(:), cl_mt_all, 'uni', 0));

tb_all_combi = array2table(mt_param_vals_combi, 'VariableNames', cl_st_param_keys);

disp(tb_all_combi);

    a       c    
    _    ________

    1           0
    2           0
    3           0
    4           0
    5           0
    1    0.055556
    2    0.055556
    3    0.055556
    4    0.055556
    5    0.055556
    1     0.11111
    2     0.11111
    3     0.11111
    4     0.11111
    5     0.11111
    1     0.16667
    2     0.16667
    3     0.16667
    4     0.16667
    5     0.16667
    1     0.22222
    2     0.22222
    3     0.22222
    4     0.22222
    5     0.22222
    1     0.27778
    2     0.27778
    3     0.27778
    4     0.27778
    5     0.27778
    1     0.33333
    2     0.33333
    3     0.33333
    4     0.33333
    5     0.33333
    1     0.38889
    2     0.38889
    3     0.38889
    4     0.38889
    5     0.38889
    1     0.44444
    2     0.44444
    3     0.44444
    4     0.44444
    5     0.44444
    1         0.5
    2         0.5
    3         0.5
    4         0.5
    5         0.5