这个ndgrid函数几乎给出了答案,但有一个警告:n必须明确定义输出变量才能调用它。自n是任意的,最好的方法是使用逗号分隔列表(从单元格数组生成的n作为输出。结果n然后将矩阵连接到所需的n-栏表:vectors = { [1 2], [3 6 9], [10 20] }; %// input data: cell array of vectorsn = numel(vectors); %// number of vectorscombs = cell(1,n); %// pre-define to generate comma-separated list[combs{end:-1:1}] = ndgrid(vectors{end:-1:1}); %// the reverse order in these two%// comma-separated lists is needed to produce the rows of the result matrix in%// lexicographical order combs = cat(n+1, combs{:}); %// concat the n n-dim arrays along dimension n+1combs = reshape(combs,[],n); %// reshape to obtain desired matrix