2

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
1
  • You need to assign the reshaped array to a new variable before you can do that... Commented Apr 28, 2016 at 7:21

2 Answers 2

2

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

1 Comment

lol ok, i thought it was a dumb question, and indeed it was thanks
0

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

1 Comment

Why the scold? I've seen plenty of answers the require further explanation. The question itself is implied. 'What's wrong or how do I do jt right?'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.