1

So I've got a numpy array like this:

a = np.array([[1, 2, 3],
              [2, 3, 4],
              [4, 5, 6]])

and I want to convert it into an array like this:

[[[1, 1, 1], [2, 2, 2], [3, 3, 3]],
 [[2, 2, 2], [3, 3, 3], [4, 4, 4]],
 [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]

How would you do it?

1 Answer 1

3

Use numpy.repeat on the array with one extra dimension:

np.repeat(a[...,None], 3, axis=2)

Or numpy.tile:

np.tile(a[...,None], (1,1,3))

Output:

array([[[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]],

       [[2, 2, 2],
        [3, 3, 3],
        [4, 4, 4]],

       [[4, 4, 4],
        [5, 5, 5],
        [6, 6, 6]]])
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.