我有两个2D(numpy)数组,我想通过以下方式从中生成3D数组:第一个数组的 n 行中的每行将(第二个)第二个数组乘以产生一个新的矩阵,产生 n 个新数组(形成3D数组)。我认为一个简单的示例将有助于理解:
$example = collect([
"tracks" => [
[
"name" => "track 1",
"segments" => [
[
"name" => "track 1 - segment 1",
"points" => [
["timestamp" => "2008-09-21T11:00:10Z", "lat"=>1, "lon"=>1],
["timestamp" => "2008-09-21T11:00:20Z", "lat"=>1, "lon"=>1],
["timestamp" => "2008-09-21T11:00:30Z", "lat"=>1, "lon"=>1],
["timestamp" => "2008-09-21T11:00:40Z", "lat"=>1, "lon"=>1],
// --- split here ("clone" 'track 1 - segment 1' between segment 1 and 2)
["timestamp" => "2008-09-21T13:00:10Z", "lat"=>1, "lon"=>1],
["timestamp" => "2008-09-21T13:00:20Z", "lat"=>1, "lon"=>1],
["timestamp" => "2008-09-21T13:00:30Z", "lat"=>1, "lon"=>1],
// --- and split here
["timestamp" => "2008-09-21T15:00:10Z", "lat"=>1, "lon"=>1],
["timestamp" => "2008-09-21T15:00:20Z", "lat"=>1, "lon"=>1],
]
],
[
"name" => "track 1 - segment 2",
"points" => []
],
],
],
[
"name" => "track 2",
"segments" => []
]
]
]);
事实是尺寸是任意的(因此不仅 2x3 和 3x3 ,当然第二个尺寸始终兼容),我正在寻找一种解决方案“用于循环”。例如,我尝试重复数组B然后相乘
A = [[a11 a12 a13]
[a21 a22 a23]]
B = [[b11 b12 b13]
[b21 b22 b23]
[b31 b32 b33]]
# The product "A*B" would result in a matrix C such as
C = [[[a11*b11 a12*b12 a13*b13]
[a11*b21 a12*b22 a13*b23]
[a11*b31 a12*b32 a13*b33]]
[[a21*b11 a22*b12 a23*b13]
[a21*b21 a22*b22 a23*b23]
[a21*b31 a22*b32 a23*b33]]]
# Which is equivalent to (in numpy notation)
C[0] = A[0]*B
C[1] = A[1]*B
但是新尺寸不兼容。
答案 0 :(得分:3)
我认为这就是您想要的:
C = A[:, None] * B