[Numpy-discussion] repeat array along new axis without making a copy

Steve Schmerler elcortogm at googlemail.com
Wed Feb 15 03:25:16 EST 2012


Hi

I'd like to repeat an array along a new axis (like broadcast):

    In [8]: a
    Out[8]: 
    array([[0, 1, 2],
           [3, 4, 5]])
    In [9]: b=repeat(a[None,...], 3, axis=0)
    In [10]: b
    Out[10]: 
    array([[[0, 1, 2],
            [3, 4, 5]],

           [[0, 1, 2],
            [3, 4, 5]],

           [[0, 1, 2],
            [3, 4, 5]]])

    In [18]: id(a); id(b[0,...]); id(b[1,...]); id(b[2,...])
    Out[18]: 40129600
    Out[18]: 39752080
    Out[18]: 40445232
    Out[18]: 40510272


Can I do this such that each sub-array b[i,...] is a view and not a copy?

Background: I'm working on a container class to store trajectory-like
data. The API requires that each array has a "time axis" (axis=0 here)
along which sub-arrays are stored which may be the same in some cases.
Then, I don't want to store redundant information if possible.

Thanks!

best,
Steve



More information about the NumPy-Discussion mailing list