Fagui Curtain
Fagui Curtain

Reputation: 1917

numpy arrays: basic questions

sorry if this question is so basic

A=np.arange(64).reshape(2,32)

array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
        49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]])

A.reshape(4,4,4)

array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]],
       [[16, 17, 18, 19],
        [20, 21, 22, 23],
        [24, 25, 26, 27],
        [28, 29, 30, 31]],

       [[32, 33, 34, 35],
        [36, 37, 38, 39],
        [40, 41, 42, 43],
        [44, 45, 46, 47]],

       [[48, 49, 50, 51],
        [52, 53, 54, 55],
        [56, 57, 58, 59],
        [60, 61, 62, 63]]])

Now, i would have liked something like A[2] or A[2,:] or A[2,:,:] to return me the matrix

[[32, 33, 34, 35],
 [36, 37, 38, 39],
 [40, 41, 42, 43],
 [44, 45, 46, 47]]

and A[2,2,2] to return me 42 for example

but i got this error

IndexError: too many indices for array

Upvotes: 2

Views: 67

Answers (2)

co2y
co2y

Reputation: 179

after A.reshape(4,4,4) , A does not change

Upvotes: 0

Mathias711
Mathias711

Reputation: 6658

You have to do

A = A.reshape(4,4,4)

instead of

A.reshape(4,4,4)

Because reshape is not inplace, you need to do this. Then you can do

A[2,2,2]
Out[301]: 42

Upvotes: 2

Related Questions