我如何在不使用循环的情况下对 3 个以上的多维数组进行元素乘法
How can i do elementwise multiplication of more than 3 multidimensional arrays without using loop
我有一个包含四个二维数组的 3 维数组
>>> print(newimagetensor) # printing the array
[[[1.06340611e+02 1.83682746e+02 2.91655784e-02 7.70948060e+01]
[3.74227522e+01 2.35463417e+01 4.74963539e+01 8.81179854e+01]
[1.01175706e+02 1.37398267e+02 1.06894601e+02 1.74730973e+02]
[5.21353237e+01 2.23919946e+02 6.98383627e+00 1.70969215e+02]]
[[1.06412725e+02 1.42465906e+02 3.57986693e+01 5.05158797e+01]
[2.04189865e+02 2.46906702e+02 7.99231654e+01 1.76542267e+02]
[2.23479234e+02 2.28124699e+02 2.16862739e+01 9.95896972e+00]
[4.33067570e+01 2.23926338e+02 2.50784426e+01 1.07382444e+02]]
[[2.44261830e+02 1.35957148e+02 1.76428664e+02 8.04564859e+01]
[1.75057737e+02 2.12829546e+02 4.66351072e+00 1.91286800e+02]
[2.52159578e+02 1.90782242e+02 7.15132180e+01 2.01266229e+02]
[2.63226317e+01 1.14212849e+02 2.31691853e+02 7.48716078e+01]]
[[7.33827113e+01 3.31572859e+01 4.93857426e+00 1.73103061e+02]
[5.39651696e+01 6.77143981e+01 1.25351156e+02 1.36074490e+01]
[1.46399989e+02 3.74157866e+01 1.50272912e+02 1.78438382e+02]
[2.60952794e+01 1.05584277e+02 1.77072040e+02 1.05615714e+02]]]
通常我可以使用循环
对这些图像进行逐元素乘法
>>> result = np.ones([4,4]) #creating a ones array equal in size to our images
>>> for i in range(len(newimagetensor)):
result *= newimagetensor[i] #Multiply all the images in the newimagetensor
>>> print(result)
输出
[[2.02834617e+08 1.17966943e+08 9.09720983e+02 5.42398960e+07]
[7.21879586e+07 8.37855764e+07 2.21908670e+06 4.04925360e+07]
[8.34699072e+08 2.23741421e+08 2.49119505e+07 6.24947437e+07]
[1.55088285e+06 6.04661303e+08 7.18547308e+06 1.45176694e+08]]
但我想在不使用循环的情况下做同样的事情,而且如果可能的话,也只用两行代码。
是否有相应的函数或库?
numpy.prod(array, axis=0)
适合我
我有一个包含四个二维数组的 3 维数组
>>> print(newimagetensor) # printing the array
[[[1.06340611e+02 1.83682746e+02 2.91655784e-02 7.70948060e+01]
[3.74227522e+01 2.35463417e+01 4.74963539e+01 8.81179854e+01]
[1.01175706e+02 1.37398267e+02 1.06894601e+02 1.74730973e+02]
[5.21353237e+01 2.23919946e+02 6.98383627e+00 1.70969215e+02]]
[[1.06412725e+02 1.42465906e+02 3.57986693e+01 5.05158797e+01]
[2.04189865e+02 2.46906702e+02 7.99231654e+01 1.76542267e+02]
[2.23479234e+02 2.28124699e+02 2.16862739e+01 9.95896972e+00]
[4.33067570e+01 2.23926338e+02 2.50784426e+01 1.07382444e+02]]
[[2.44261830e+02 1.35957148e+02 1.76428664e+02 8.04564859e+01]
[1.75057737e+02 2.12829546e+02 4.66351072e+00 1.91286800e+02]
[2.52159578e+02 1.90782242e+02 7.15132180e+01 2.01266229e+02]
[2.63226317e+01 1.14212849e+02 2.31691853e+02 7.48716078e+01]]
[[7.33827113e+01 3.31572859e+01 4.93857426e+00 1.73103061e+02]
[5.39651696e+01 6.77143981e+01 1.25351156e+02 1.36074490e+01]
[1.46399989e+02 3.74157866e+01 1.50272912e+02 1.78438382e+02]
[2.60952794e+01 1.05584277e+02 1.77072040e+02 1.05615714e+02]]]
通常我可以使用循环
对这些图像进行逐元素乘法>>> result = np.ones([4,4]) #creating a ones array equal in size to our images
>>> for i in range(len(newimagetensor)):
result *= newimagetensor[i] #Multiply all the images in the newimagetensor
>>> print(result)
输出
[[2.02834617e+08 1.17966943e+08 9.09720983e+02 5.42398960e+07]
[7.21879586e+07 8.37855764e+07 2.21908670e+06 4.04925360e+07]
[8.34699072e+08 2.23741421e+08 2.49119505e+07 6.24947437e+07]
[1.55088285e+06 6.04661303e+08 7.18547308e+06 1.45176694e+08]]
但我想在不使用循环的情况下做同样的事情,而且如果可能的话,也只用两行代码。 是否有相应的函数或库?
numpy.prod(array, axis=0)
适合我