tanay
tanay

Reputation: 458

Reshaping a ndarray efficiently

I have a ndarray(example given below).

A=
[[0.1  1.1 ]
 [0.1  1.3 ]
 [0.25 1.25]
 [0.25 1.45]
 [0.37 1.37]
 [0.35 1.8 ]]

I want to reshape it as

B=
[[[0.1  1.1 ] [0.1  1.3 ]]
 [[0.25  1.25 ] [0.25  1.45 ]]
 [[0.37  1.37 ] [0.35  1.38 ]]]

so that doing

B[0] gives me [[0.1  1.1 ] [0.1  1.3 ]] and B[0][0] gives [0.1  1.1 ]

Upvotes: 0

Views: 41

Answers (2)

Nik P
Nik P

Reputation: 224

The following should work

A.reshape(3,2,2)

Upvotes: 1

Jan Christoph Terasa
Jan Christoph Terasa

Reputation: 5935

Use standard numpy reshaping:

B = A.reshape(-1, 2, 2)

Upvotes: 1

Related Questions