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

Home » Public Forums » archive » IDL array subscripting issue
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
IDL array subscripting issue [message #90225] Thu, 12 February 2015 09:22 Go to next message
David B is currently offline  David B
Messages: 18
Registered: January 2015
Junior Member
Now this is probably a simple and quick problem for someone with experience. I think I may have missed the point or fallen victim to a problem with an obvious solution.
===============================

IDL> a = INDGEN(10)
IDL> b = [0,10]
IDL> print, a[b]
0 9
IDL> b = [0,9]
IDL> print, a[b]
0 9

===============================
Why?!

David B
Re: IDL array subscripting issue [message #90226 is a reply to message #90225] Thu, 12 February 2015 09:23 Go to previous messageGo to next message
David B is currently offline  David B
Messages: 18
Registered: January 2015
Junior Member
By the way as an extension:

IDL> print, a[10]
% Attempt to subscript A with <INT ( 10)> is out of range.
% Execution halted at: CLOUDWALKER 556
Re: IDL array subscripting issue [message #90227 is a reply to message #90225] Thu, 12 February 2015 09:29 Go to previous messageGo to next message
Fabzi is currently offline  Fabzi
Messages: 305
Registered: July 2010
Senior Member
On 12.02.2015 18:22, David B wrote:
> ===============================
>
> IDL> a = INDGEN(10)
> IDL> b = [0,10]
> IDL> print, a[b]
> 0 9
> IDL> b = [0,9]
> IDL> print, a[b]
> 0 9
>
> ===============================
> Why?!

This is a strange but documented feature of IDL:

http://www.exelisvis.de/docs/Using_Arrays_as_Array_Su.html

See "Array Subscripts and Clipping".

Depending on what you want, this is a feature of a bug. I learned to
like it as a feature, personally.

Cheers,

Fabien
Re: IDL array subscripting issue [message #90228 is a reply to message #90225] Thu, 12 February 2015 10:05 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
David B writes:

>
> Now this is probably a simple and quick problem for someone with experience. I think I may have missed the point or fallen victim to a problem with an obvious solution.
> ===============================
>
> IDL> a = INDGEN(10)
> IDL> b = [0,10]
> IDL> print, a[b]
> 0 9
> IDL> b = [0,9]
> IDL> print, a[b]
> 0 9
>
> ===============================
> Why?!

Here is an explanation:

http://www.idlcoyote.com/code_tips/oobindices.html

This can be a particularly pernicious "bug" to track down in your code.
Personally, I've lost a lot of hair over this one. :-(

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: IDL array subscripting issue [message #90229 is a reply to message #90227] Thu, 12 February 2015 10:26 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
On 02/12/15 12:29, Fabien wrote:
> On 12.02.2015 18:22, David B wrote:
>> ===============================
>>
>> IDL> a = INDGEN(10)
>> IDL> b = [0,10]
>> IDL> print, a[b]
>> 0 9
>> IDL> b = [0,9]
>> IDL> print, a[b]
>> 0 9
>>
>> ===============================
>> Why?!
>
> This is a strange but documented feature of IDL:
>
> http://www.exelisvis.de/docs/Using_Arrays_as_Array_Su.html
>
> See "Array Subscripts and Clipping".
>
> Depending on what you want, this is a feature of a bug. I learned to
> like it as a feature, personally.

I really like the use of negative indices to reference from the end of
the array. The above url has the following "note" in the section about
clipping:

<quote>
Note: Because of the confusion between the clipping of array subscripts
and the use of negative indices (see the next section), it is best to
avoid using code that relies on this clipping behavior.
</quote>

Just sayin'. Not judging.

cheers,

paulv
Re: IDL array subscripting issue [message #90230 is a reply to message #90229] Thu, 12 February 2015 10:36 Go to previous messageGo to next message
Fabzi is currently offline  Fabzi
Messages: 305
Registered: July 2010
Senior Member
On 12.02.2015 19:26, Paul van Delst wrote:
>
> I really like the use of negative indices to reference from the end of
> the array. The above url has the following "note" in the section about
> clipping:
>
> <quote>
> Note: Because of the confusion between the clipping of array subscripts
> and the use of negative indices (see the next section), it is best to
> avoid using code that relies on this clipping behavior.
> </quote>
>
> Just sayin'. Not judging.

Haha, I knew this one would come, and I agree

I mean that I like the fact that array[[-1,2,3,-1]] is not throwing an
error, even before negative indices were added to IDL.

say that you're code relies on following:

inds = [-1,2,3,-1]
out = arr[inds]
p = where(inds lt 0, cnt)
if cnt ne 0 then out[p] = !VALUES.F_NAN

How would write the snipset above with
STRICTARRSUBS compile_opt?
Re: IDL array subscripting issue [message #90238 is a reply to message #90225] Fri, 13 February 2015 01:32 Go to previous message
David B is currently offline  David B
Messages: 18
Registered: January 2015
Junior Member
Thanks all!

The can cause issue say when you concatenate new elements onto your array upon which the index is subscripting, as then your array refernce becomes incorrect. I have just handled the case on its own and shifted the array value down to its proper value so:

index[WHERE(index EQ n_elements(array)]--

David
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Variable attributes and undef vars
Next Topic: CURVFIT with XY error

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

Current Time: Wed Oct 08 09:19:21 PDT 2025

Total time taken to generate the page: 0.00603 seconds