NaN
NaN

Reputation: 691

How can I repeat this numpy array within itself?

I have an array z of shape (8,):

>>> z
array([-30000.        , -30000.        , -30000.        , -30000.        ,
       -27703.12304688, -27703.15429688, -27703.70703125, -27703.67382812])

I would like to copy the values 7 more times while maintaining their positions, to create an array zr of shape (8,8) e.g.:

    >>> z
    array([-30000.        , -30000.        , -30000.        , -30000.        ,
           -27703.12304688, -27703.15429688, -27703.70703125, -27703.67382812],
          [-30000.        , -30000.        , -30000.        , -30000.        ,
           -27703.12304688, -27703.15429688, -27703.70703125, -27703.67382812]
            .........)

I've tried np.repeat() but this creates an array of shape (64,) and I would like (8,8).

>>> zr = np.repeat(z, 8)
>>> zr
array([-30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -30000.        , -30000.        , -30000.        , -30000.        ,
       -27703.12304688, -27703.12304688, -27703.12304688, -27703.12304688,
       -27703.12304688, -27703.12304688, -27703.12304688, -27703.12304688,
       -27703.15429688, -27703.15429688, -27703.15429688, -27703.15429688,
       -27703.15429688, -27703.15429688, -27703.15429688, -27703.15429688,
       -27703.70703125, -27703.70703125, -27703.70703125, -27703.70703125,
       -27703.70703125, -27703.70703125, -27703.70703125, -27703.70703125,
       -27703.67382812, -27703.67382812, -27703.67382812, -27703.67382812,
       -27703.67382812, -27703.67382812, -27703.67382812, -27703.67382812])
>>> zr.shape
(64,)

What am I doing wrong?

Upvotes: 1

Views: 462

Answers (3)

hpaulj
hpaulj

Reputation: 231385

In [278]: z = np.arange(4)   

repeat without axis just replicates each element in the flat order

In [280]: np.repeat(z,4)                                                                               
Out[280]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])

but that can be massaged into your desired array:

In [281]: np.repeat(z,4).reshape(4,4)                                                                  
Out[281]: 
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3]])
In [282]: np.repeat(z,4).reshape(4,4).T                                                                
Out[282]: 
array([[0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3]])

If z is (1,n) then we can repeat on the first axis:

In [283]: np.repeat(z[None,:],4,0)                                                                     
Out[283]: 
array([[0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3]])

np.tile may be easier to use. Internally it uses repeat.

Upvotes: 0

cs95
cs95

Reputation: 402493

Use np.tile with a list to return a a 2D array:

# tile improvement courtesy OP
np.tile(z, [8, 1])

If you want a read-only view, np.broadcast_to is quite fast:

np.broadcast_to(z, (8,)+z.shape)

Upvotes: 1

w8eight
w8eight

Reputation: 623

You can try this

np.tile(z,8).reshape(8,8)

Upvotes: 0

Related Questions