0

I have an array:

one = np.array([[1], [0]])

How arr can be created such that, every element of arr would be equal to one?

If I do:

arr = one * 4

Due to broadcasting above statement doesn't give the desired behavior.

2
  • What's the shape of arr supposed to be? With lists * 4 is a replication, but with arrays it is plain numeric multiplication. Commented Mar 3, 2019 at 22:17
  • @hpaulj I accepted the answer, it does exactly what I wanted Commented Mar 3, 2019 at 22:20

1 Answer 1

2

Use repeat:

np.repeat(one[np.newaxis, ...], 4, axis=0)

Alternatively, you can try broadcast_to. This will be faster but read-only:

np.broadcast_to(one, (4, *one.shape))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.