2

I have a 3D np arrays like this :

 x= array([[[  1.,   2.,   3.],
    [  4.,   5.,   6.],
    [  7.,   8.,   9.],
    [ 10.,  11.,  12.]],

   [[ 13.,  14.,  15.],
    [ 16.,  17.,  18.],
    [ 19.,  20.,  21.],
    [ 22.,  23.,  24.]]])

I would like to repeat my array n times ( eg 3 times) like this :

array([[[  1.,   2.,   3.],
    [  4.,   5.,   6.],
    [  7.,   8.,   9.],
    [ 10.,  11.,  12.]],

   [[  1.,   2.,   3.],
    [  4.,   5.,   6.],
    [  7.,   8.,   9.],
    [ 10.,  11.,  12.]],

   [[  1.,   2.,   3.],
    [  4.,   5.,   6.],
    [  7.,   8.,   9.],
    [ 10.,  11.,  12.]],

   [[ 13.,  14.,  15.],
    [ 16.,  17.,  18.],
    [ 19.,  20.,  21.],
    [ 22.,  23.,  24.]],

    [[ 13.,  14.,  15.],
    [ 16.,  17.,  18.],
    [ 19.,  20.,  21.],
    [ 22.,  23.,  24.]]

   [[ 13.,  14.,  15.],
    [ 16.,  17.,  18.],
    [ 19.,  20.,  21.],
    [ 22.,  23.,  24.]]])

I have tried like this :

xx=np.vstack([x]*3)
print xx.reshape(6,4,3)


array([[[  1.,   2.,   3.],
    [  4.,   5.,   6.],
    [  7.,   8.,   9.],
    [ 10.,  11.,  12.]],

   [[ 13.,  14.,  15.],
    [ 16.,  17.,  18.],
    [ 19.,  20.,  21.],
    [ 22.,  23.,  24.]],

   [[  1.,   2.,   3.],
    [  4.,   5.,   6.],
    [  7.,   8.,   9.],
    [ 10.,  11.,  12.]],

   [[ 13.,  14.,  15.],
    [ 16.,  17.,  18.],
    [ 19.,  20.,  21.],
    [ 22.,  23.,  24.]],

   [[  1.,   2.,   3.],
    [  4.,   5.,   6.],
    [  7.,   8.,   9.],
    [ 10.,  11.,  12.]],

   [[ 13.,  14.,  15.],
    [ 16.,  17.,  18.],
    [ 19.,  20.,  21.],
    [ 22.,  23.,  24.]]])

How can I get in the order what I want, there should be the easy way to do this. Thanks in advance for your suggestions.

2 Answers 2

4

After a bit of trial and error I have found a way to do it:

np.tile(x.reshape(2,12), [1,3]).reshape(6,4,3)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use np.repeat with axis = 0:

np.repeat(x, [3, 3], axis = 0)  # or more generally np.repeat(x, [n] * len(x), axis = 0)
                                # here n is the repeat times
Out[514]:
array([[[  1.,   2.,   3.],
        [  4.,   5.,   6.],
        [  7.,   8.,   9.],
        [ 10.,  11.,  12.]],

       [[  1.,   2.,   3.],
        [  4.,   5.,   6.],
        [  7.,   8.,   9.],
        [ 10.,  11.,  12.]],

       [[  1.,   2.,   3.],
        [  4.,   5.,   6.],
        [  7.,   8.,   9.],
        [ 10.,  11.,  12.]],

       [[ 13.,  14.,  15.],
        [ 16.,  17.,  18.],
        [ 19.,  20.,  21.],
        [ 22.,  23.,  24.]],

       [[ 13.,  14.,  15.],
        [ 16.,  17.,  18.],
        [ 19.,  20.,  21.],
        [ 22.,  23.,  24.]],

       [[ 13.,  14.,  15.],
        [ 16.,  17.,  18.],
        [ 19.,  20.,  21.],
        [ 22.,  23.,  24.]]])

Another option would be to index it as:

x[[0,0,0,1,1,1]]

Or programmatically:

x[[i for i in range(len(x)) for j in range(3)]]
Out[518]:
array([[[  1.,   2.,   3.],
        [  4.,   5.,   6.],
        [  7.,   8.,   9.],
        [ 10.,  11.,  12.]],

       [[  1.,   2.,   3.],
        [  4.,   5.,   6.],
        [  7.,   8.,   9.],
        [ 10.,  11.,  12.]],

       [[  1.,   2.,   3.],
        [  4.,   5.,   6.],
        [  7.,   8.,   9.],
        [ 10.,  11.,  12.]],

       [[ 13.,  14.,  15.],
        [ 16.,  17.,  18.],
        [ 19.,  20.,  21.],
        [ 22.,  23.,  24.]],

       [[ 13.,  14.,  15.],
        [ 16.,  17.,  18.],
        [ 19.,  20.,  21.],
        [ 22.,  23.,  24.]],

       [[ 13.,  14.,  15.],
        [ 16.,  17.,  18.],
        [ 19.,  20.,  21.],
        [ 22.,  23.,  24.]]])

2 Comments

Starting with a (2,4,3) you shouldn't need the reshape after repeat.
@hpaulj Thanks for the comment. It works with [3,3] along axis = 0 without reshape.

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.