简体   繁体   English

MATLAB中ndgrid中可变数量的参数

[英]variable number of arguments in ndgrid in MATLAB

I would like to generate a matrix of references. 我想生成一个参考矩阵。 This code works but I would like to use a variable size of set. 此代码有效,但我想使用可变大小的set。 I can't manage that... Thanks for helping ! 我无法解决...谢谢您的帮助!

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 ?

Use a cell -array GRID on the receiving end, to get a programmatic comma-separated list : 在接收端使用cell -array GRID获得程序化的逗号分隔列表

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

(No need to declare GRID = cell(..) first.) (无需先声明GRID = cell(..) 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM