Assume I have a four-dimensional matrix A(:, :, :, :). I want to update the matrix by performing some processing on it. The pseudo codes are presented as follows:
for ii = 1:m
for jj = 1:n
A = myFunction(A(:,:,jj,ii))
end
end
To implement the for-loop processing in Python:
for ii in range(m):
for jj in range(n):
A = myFunction(A[:,:,jj,ii])
Is that correct?
asked Oct 26, 2016 at 22:21
jwm
5,07812 gold badges55 silver badges88 bronze badges
1 Answer 1
If you have 4-dimensional matrix, you should use 4 indexes:
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])
For example:
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)
answered Oct 26, 2016 at 23:02
Pavel
5,9665 gold badges38 silver badges60 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
kkandll.