Matlab 中用于 Mathematica 的元组的等效函数?

Equivalent Function in Matlab for Tuples for Mathematica?

Matlab 中是否有与 Mathematica 中元组等效的函数?

见第一个例子:

http://reference.wolfram.com/language/ref/Tuples.html

我只是指输出,不一定是大括号。

谢谢。

正如我在评论中所说,你只需要适应this answer。您可以按如下方式进行:

function y = tuples(x, n)
y = cell(1,n);
[y{end:-1:1}] = ndgrid(x);
y = cat(n+1, y{:});
y = reshape(y, [], n);

这给出了一个矩阵,其中每一行都是一个元组。例如:

>> tuples([1 2 5], 2)
ans =
     1     1
     1     2
     1     5
     2     1
     2     2
     2     5
     5     1
     5     2
     5     5