6

Provided a 1D array as a:

a=np.arange(8)

I would like it to be reproduced in a 3D scheme in order to have such shape (n1, len(a), n3). Is there any working way to obtain this via np.tile? It seems trivial, but trying:

np.shape(  np.tile(a, (n1,1,n3))  )

or

np.shape(  np.tile( np.tile(a, (n1,1)), (1,1,n2) )  )

I never obtain what I need, being the resulting shapes (n1, 1, len(a)*n3) or (1, n1, len(a)*n3). Maybe it is me not understanding how tile works ...

1 Answer 1

9

What's happening is that a is being made a 1x1x8 array before the tiling is applied. You'll need to make a a 1x8x1 array and then call tile.

As the documentation for tile notes:

If A.ndim < d, A is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired behavior, promote A to d-dimensions manually before calling this function.

The easiest way to get the result you're after is to slice a with None (or equivalently, np.newaxis) to make it the correct shape.

As a quick example:

import numpy as np

a = np.arange(8)
result = np.tile(a[None, :, None], (4, 1, 5))
print result.shape
Sign up to request clarification or add additional context in comments.

Comments

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.