【问题标题】:variable number of arguments in ndgrid in MATLABMATLAB中ndgrid中可变数量的参数
【发布时间】:2015-05-02 05:11:20
【问题描述】:

我想生成一个参考矩阵。此代码有效,但我想使用可变大小的集合。我无法做到...感谢您的帮助!

cpt = 1;
for ll = 1: 3
    nb_rules = 5;
    sets{cpt} = [1 : nb_rules];
    cpt = cpt +1;
end

[x y z] = ndgrid(sets{:});% Here begins the trouble :
mat_ref = [x(:) y(:) z(:)];% what if size is not 3 ?

【问题讨论】:

    标签: matlab cartesian-product


    【解决方案1】:

    在接收端使用cell-array GRID,得到一个程序化的comma-separated list

    N = numel(sets);
    [GRID{1:N}] = ndgrid(sets{:});
    mat_ref = reshape(cat(N+1,GRID{:}),[],N)
    

    (无需先声明GRID = cell(..)。)

    【讨论】:

    • 非常感谢您的这一课!!太完美了!
    • @BPulsart:如果此答案解决了您的问题,请考虑通过单击旁边的灰色复选标记将其变为绿色来接受它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多