Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I couldn't understand how to put right indexing for 3d array

import numpy as np
import random
m,n,p=5,5,4
arr=np.zeros((p,n,m))

def insertingArray(arr):
    for k in range(p):
        for i in range(m):
            for j in range(n):
                arr[i][j][k]=random.randint(1,10)

    
    

insertingArray(arr)
print(arr)

This is the output after running.

1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You confused with the sizes so you get index out of range error

try:

import numpy as np
import random
m,n,p=5,5,4
arr=np.zeros((p,n,m))

def insertingArray(arr):
    for k in range(p):
        for i in range(n):
            for j in range(m):
                arr[k][i][j]=random.randint(1,10)

    
    

insertingArray(arr)
print(arr)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading