comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Negative array indices
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Negative array indices [message #73810] Thu, 02 December 2010 14:15
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 12/2/10 2:54 PM, b_gom wrote:
> I've been stumbling around trying to figure out uses for the negative
> array indexing in IDL 8. As usual, the documentation doesn't fully
> describe the logic behind the syntax rules.
>
> One rule that I've discovered is that negative array indices shouldn't
> be thought of as indices 'rolling over' as you cross zero. For
> example:
> If x=findgen(10), then x[-1] is the same as x[9], and x[-10] is the
> same as x[0], but x[-11] isn't allowed. I can see why this follows
> from not allowing positive indices greater than the number of elements
> in x.
>
> I guess this leads to this behaviour:
> IDL> x=findgen(10)
> IDL> print,x[-4:0]
> % Illegal subscript range: X.
>
> because this gets translated to:
> IDL> print,x[6,0]
> which isn't allowed.
>
> Now, on to the real question. Why does the following not work:
>
> IDL> print,x[1:3]
> 1.00000 2.00000 3.00000
> IDL> print,x[[1,2,3]]
> 1.00000 2.00000 3.00000
> IDL> print,x[-3:-1]
> 7.00000 8.00000 9.00000
> IDL> print,x[[-3,-2,-1]]
> 0.000000 0.000000 0.000000

It's a bit of a strange story, but this last one "print, x[[-3,-2,-1]]"
was actually allowed before IDL 8.0, so the behavior had to match up to
maintain backward compatibility. It had nothing to do with counting back
from the end of the array, instead it just checked bounds of index
arrays and used the closest valid index, in your case 0. It would work
similarly if you did:

IDL> print, x[[10, 10, 10]]
9.00000 9.00000 9.00000

Mike
--
www.michaelgalloy.com
Research Mathematician
Tech-X Corporation
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Making a ginormous 54 panel plot...
Next Topic: IDLWAVE: Wrong type argument: consp, nil

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 17:06:22 PDT 2025

Total time taken to generate the page: 0.00455 seconds