[Numpy-discussion] yet another trivial question

Joe Kington jkington@wisc....
Fri Nov 2 10:16:21 CDT 2012


On Fri, Nov 2, 2012 at 9:18 AM, Neal Becker <ndbecker2@gmail.com> wrote:

> I'm trying to convert some matlab code.  I see this:
>
> b(1)=[];
>
> AFAICT, this removes the first element of the array, shifting the others.
>
> What is the preferred numpy equivalent?
>
> I'm not sure if
>
> b[:] = b[1:]
>

Unless I'm missing something, don't you just want:

    b = b[1:]


>
> is safe or not
>


It's not exactly the same as the matlab equivalent, as matlab will always
make a copy, and this will be a view of the same array.  For example, if
you do something like this:

    import numpy as np

    a = np.arange(10)

    b = a[1:]

    b[3] = 1000

    print a
    print b

You'll see that modifying "b" in-place will modify "a" as well, as "b" is
just a view into "a".  This wouldn't be the case in matlab (if I remember
correctly, anyway...).

> _________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20121102/9f9c9171/attachment.html 


More information about the NumPy-Discussion mailing list