我想采用任意1D向量 a = [k] 和 b = [m] 并形成有序对的矩阵 c = [2 xkxm] ,$c->(:,(i),(j)) = [ $a->(i), $b->(j) ]
。即 a 和 b a.k.a中笛卡尔积的所有有序元素对的集合。
当然我可以使用循环和[glue]函数来实现这一点,但这不符合Perl / PDL的精神。是否有一种奇特的方法,涉及切片,虚拟尺寸和胶水,让我在那里?
另外,使用Math :: Cartesian :: Product(在这里回答:In Perl, how can I get the Cartesian product of multiple sets?是作弊!:3我想要直接perl / PDL并希望在此过程中学到一些东西。
答案 0 :(得分:2)
我得到的东西符合我的标准:
my $a = pdl 1,2,3,4;
my $b = pdl 5,6,7;
print "a = $a\n";
print "b = $b\n";
print "dummy dimensioned:\n";
$a = $a->dummy(0,$b->dim(0));
print "a".$a->shape." = $a\n";
$b = $b->dummy(0, $a->dim(1))->transpose;
print "b".$b->shape." = $b\n";
print "Glued together:\n"
my $c = $a->dummy(0,1)->glue(0, $b->dummy(0,1));
print "c".$c->shape." = $c\n";
a = [1 2 3 4]
b = [5 6 7]
dummy dimensioned:
a[3 4] =
[
[1 1 1]
[2 2 2]
[3 3 3]
[4 4 4]
]
b[3 4] =
[
[5 6 7]
[5 6 7]
[5 6 7]
[5 6 7]
]
Glued together:
c[2 3 4] = [[[1 5][1 6][1 7]][[2 5][2 6][2 7]][[3 5][3 6][3 7]][[4 5][4 6][4 7]]]