0

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?

7
  • 1
    To process an n-dimensional array, you need n nested loops. Commented Oct 26, 2016 at 22:36
  • can you show the code to realize the 4-D matrix? Commented Oct 26, 2016 at 22:38
  • it's the same as for 2-D, just add loops for kk and ll. Commented Oct 26, 2016 at 22:39
  • for multi-dimension matrix and these kind of loops, ndarray from numpy would be best choice. Commented Oct 26, 2016 at 22:41
  • You mean the code structure is valid in Python? It seems I need to make the index range from 0 to max-1 and delete the two 'end' . I am new to Python programming. Commented Oct 26, 2016 at 22:42

1 Answer 1

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) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.