How to make multidimensional array using np.repeat

Viewed 33

I have an array A = np.array([1.43, 5.12, 2.67]) I want to make a multidimensional array named B from array A using np.repeat like this

B =np.repeat(A, 4)

and the result show like this:

B = [1.43, 1.43, 1.43, 1.43, 5.12, 5.12, 5.12, 5.12, 2.67, 2.67, 2.67, 2.67)

But i want the result shown like this:

B = ([1. 43, 1.43, 1.43, 1.43],
[5.12, 5.12, 5.12, 5.12],
[2.67, 2.67, 2.67, 2.67])

What should I do to get the result like that?

1 Answers

You can reshape the output array to achieve the desired format.

import numpy as np

A = np.array([1.43, 5.12, 2.67])
B = np.repeat(A, 4).reshape(3, -1)
print(B)

Output

[[1.43 1.43 1.43 1.43]
 [5.12 5.12 5.12 5.12]
 [2.67 2.67 2.67 2.67]]
Related