简体   繁体   中英

numpy.tile for 3d array

I have to get 3D array demension (4, 2, 1) from two value (60, 80)

import numpy as np
array([[[ 60.],
        [ 60.]],
   [[ 60.],
    [ 60.]],
   [[ 80.],
    [ 80.]],
   [[ 80.],
    [ 80.]]])

I do as below

a = np.array([[[ 60.]],
       [[ 80.]]])
np.tile(a, (2, 2,1))

and get

array([[[ 60.],
        [ 60.]],
       [[ 80.],
        [ 80.]],
       [[ 60.],
        [ 60.]],
       [[ 80.],
        [ 80.]]])

How to get the expected array?

You might use combined np.tile with np.repeat :

a = np.array([[[ 60.]],
       [[ 80.]]])
np.repeat(np.tile(a, (1,2,1)), 2, axis=0)

#array([[[ 60.],
#        [ 60.]],

#       [[ 60.],
#        [ 60.]],

#       [[ 80.],
#        [ 80.]],

#       [[ 80.],
#        [ 80.]]])

Or use np.repeat + reshape :

np.repeat(a, 4).reshape(4,2,1)
#array([[[ 60.],
#        [ 60.]],

#       [[ 60.],
#        [ 60.]],

#       [[ 80.],
#        [ 80.]],

#       [[ 80.],
#        [ 80.]]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM