假设我有一个四维矩阵A(:,:,:,:)。我想通过对矩阵执行一些处理来更新矩阵。伪代码如下:
for ii = 1:m
for jj = 1:n
A = myFunction(A(:,:,jj,ii))
end
end
在Python中实现for循环处理:
for ii in range(m):
for jj in range(n):
A = myFunction(A[:,:,jj,ii])
这是对的吗?
答案 0 :(得分:1)
如果你有4维矩阵,你应该使用4个索引:
for i in range(m):
for j in range(n):
for k in range(p):
for l in range(q):
myFunction(A[i,j,k,l])
例如:
A = [[[[6,1],[4,3]],[[4,8],[0,9]]],[[[1,5],[3,9]],[[5,5],[2,7]]]]
s = 0
for i in range(2):
for j in range(2):
for k in range(2):
for l in range(2):
s += A[i][j][k][l]
print(s)