[Numpy-discussion] yet another trivial question

David Warde-Farley d.warde.farley at gmail.com
Fri Nov 2 15:31:12 EDT 2012


On Fri, Nov 2, 2012 at 11:16 AM, Joe Kington <jkington at wisc.edu> wrote:

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

AFAIK that's correct. The closest thing to Matlab's semantics would
probably "b = b[1:].copy()".

b[:] = b[1:] would probably result in an error, as the RHS has a
first-dimension shape one less than the LHS.

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121102/15e15f43/attachment.html>


More information about the NumPy-Discussion mailing list