1

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?

1
  • Just .reshape((8, 8)) Commented Jun 6, 2019 at 15:26

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, also I used np.tile(z, [8, 1]) and that worked too
0

You can try this

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

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.