有一个名为CARTPROD的可下载函数,它给出了给定向量的笛卡尔积(link to CARTPROD function)
例如
cartprod(1:3,1:3)
ans =
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
但是,有没有一种方法可以指定在笛卡尔积中读取给定矢量的次数。我想要这样的东西:
%If user chooses the vector to be used 4 times
cartprod(1:3,1:3,1:3, 1:3)
%If user chooses the vector to be used 2 times
cartprod(1:3,1:3)
我已经尝试过考虑它,但除了手动之外,我想不出任何方式。谢谢!
你要找的是comma separated lists。没试过这个,但试试吧
myvec={1:3,1:3,1:3,1:3};
cartprod(myvec{:}); %get cartprod of all vectors in the cell-array.
或者@Sardar_Usama指出,你可以用这个替换myvec={1:3,1:3,1:3,1:3}
:
n=4; %number of repeated vectors
myvec=repmat({1:3},1,n); %repeat cell-array {1:3} 4 times