Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unexpected collapse of dimension when calling np.tile()

I am creating a multi-dimension numpy matrix like this:

a = np.array([255, 0])                            
mins_and_maxes = np.tile(a, [9, 2, 43]) 

I’m expecting mins_and_maxes to be a 4-D array with a shape of (9, 2, 43, 2). However, mins_and_maxes has a shape of (9, 2, 86). The [255, 0] arrays are sort of being ‘dissolved’. (I can’t think of a better word. "Exploded"?)

How do I get a matrix of size (9, 2, 43) where every element is a copy of the array of length 2, [255, 0]?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try:

a = np.array([255, 0])

mins_and_maxes = np.tile(a, [9, 2, 43, 1])

mins_and_maxes.shape
#(9, 2, 43, 2)

mins_and_maxes

#array([[[[255,   0],
         [255,   0],
         [255,   0],
         ...,
         [255,   0],
         [255,   0],
         [255,   0]],
       [[[255,   0],
         [255,   0],
         [255,   0],
         ...,
         [255,   0],
         [255,   0],
         [255,   0]],

        [[255,   0],
         [255,   0],
         [255,   0],
         ...,
         [255,   0],
         [255,   0],
         [255,   0]]],


       [[[255,   0],
         [255,   0],
         [255,   0],
         ...,
         [255,   0],
         [255,   0],
         [255,   0]],

        [[255,   0],
         [255,   0],
         [255,   0],
         ...,
         [255,   0],
         [255,   0],
         [255,   0]]]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading