我在numpy 3D Matrix中打印列时遇到问题。 以下是问题的简化版本:
import numpy as np
Matrix = np.zeros(((10,9,3))) # Creates a 10 x 9 x 3 3D matrix
Matrix[2][2][6] = 578
# I want to print Matrix[2][x][6] for x in range(9)
# the purpose of this is that I want to get all the Values in Matrix[2][x][6]
如果你们能帮助我,我们非常感激。 提前谢谢。
答案 0 :(得分:3)
切片可行:
a = np.zeros((10, 9, 3))
a[6, 2, 2] = 578
for x in a[6, :, 2]:
print(x)
输出:
0.0
0.0
578.0
0.0
0.0
0.0
0.0
0.0
0.0
答案 1 :(得分:1)
不确定Numpy是否支持此功能,但您可以通过这种方式使用普通列表:
如果您有三个列表a = [1,2,3]
,b = [4,5,6]
和c = [7,8,9]
,则可以通过
[2,5,8]
list(zip(a,b,c))[1]
编辑:
事实证明,这在Numpy中非常简单。根据{{3}},您可以这样做:
Matrix[:,1]
答案 2 :(得分:1)
不确定这是否是你想要的。这是演示:
In [1]: x = np.random.randint(0, 20, size=(4, 5, 3))
In [2]: x
Out[2]:
array([[[ 5, 13, 9],
[ 8, 16, 5],
[15, 17, 1],
[ 6, 14, 5],
[11, 13, 9]],
[[ 5, 8, 0],
[ 8, 15, 5],
[ 9, 2, 13],
[18, 4, 14],
[ 8, 3, 13]],
[[ 3, 7, 4],
[15, 11, 6],
[ 7, 8, 14],
[12, 8, 18],
[ 4, 2, 8]],
[[10, 1, 16],
[ 5, 2, 1],
[11, 12, 13],
[11, 9, 1],
[14, 5, 1]]])
In [4]: x[:, 2, :]
Out[4]:
array([[15, 17, 1],
[ 9, 2, 13],
[ 7, 8, 14],
[11, 12, 13]])