mario119
mario119

Reputation: 353

How to make array like this?

I want to modify my array by using like np.tile other than using for loop.

a = np.array(range(1,50)).reshape(-1,1)
a_new = np.tile(a, (100,1))
print(a_new)

will print out


[[1]
[2]
[3]
...
[48]
[49]
[50]
]

There will be 100 of [1][2]...[49][50], right? However, I want to make a_new like this below.

[[1]
[1]
[1]
...
[2]
[2]
[2]
...
[49]
[49]
[49]
...
[50]
[50]
[50]]

I want to create an array a_new as 100 of [1], 100 of [2], ... 100 of [50]. How can I do this?

Upvotes: 1

Views: 65

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Try this:

np.repeat(a, 100).reshape(-1, 1)

output:

array([[ 1],
       [ 1],
       [ 1],
       ...,
       [49],
       [49],
       [49]])

Upvotes: 1

Related Questions