Academia
Academia

Reputation: 4124

Constructing numpy array using tile

My question is: How can I get b from a using tile?

a = np.array([[1,2,-6],[-4,5,6],[10,8,-1]])

b = np.array([
          [[1,2,-6],[1,2,-6],[1,2,-6]],
          [[-4,5,6],[-4,5,6],[-4,5,6]],
          [[10,8,-1],[10,8,-1],[10,8,-1]]
         ])

I did it like this, but I want something better:

b = np.repeat(a, 3, axis=0).reshape(3,3,3)

Upvotes: 1

Views: 960

Answers (2)

Benjamin
Benjamin

Reputation: 11860

You already have the good syntax for tile: b = np.tile(a,3).reshape((3,3,3))

Upvotes: 1

Bi Rico
Bi Rico

Reputation: 25813

You could use broadcasting:

b = a.reshape((3,1,3)) * np.ones((1,3,1))

Upvotes: 1

Related Questions