简体   繁体   中英

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. 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 :

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

(No need to declare GRID = cell(..) first.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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