[Numpy-discussion] yet another trivial question

Joe Kington jkington at wisc.edu
Fri Nov 2 11:16:21 EDT 2012

On Fri, Nov 2, 2012 at 9:18 AM, Neal Becker <ndbecker2 at 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 at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121102/9f9c9171/attachment.html>

More information about the NumPy-Discussion mailing list