Skullgreymon
Skullgreymon

Reputation: 193

Restructuring the shape of an array in Python

I have an array with shape (64,64) in Python and I want to repeat these elements (three times) in the way that I could have an array with the shape (64,64,3). Any idea?

Upvotes: 0

Views: 323

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477318

Probably the most simplest way to accomplish this here is by using numpy.dstack:

import numpy as np

b = np.dstack((a, a, a))

where a was the original array (shape 64×64), and b is the new array (shape 64×64×3).

Upvotes: 1

Related Questions