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

Home » Public Forums » archive » Re: Array subscript question
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Array subscript question [message #19203 is a reply to message #19199] Fri, 03 March 2000 00:00 Go to previous messageGo to previous message
John-David T. Smith is currently offline  John-David T. Smith
Messages: 384
Registered: January 2000
Senior Member
"Kenneth P. Bowman" wrote:
>
> Can someone explain this behavior to me? I can't find anything in the
> documentation that states that repeated subscripts are handled
> differently.
>
> IDL> a = FINDGEN(5)
> IDL> i = [1, 2, 3]
> IDL> a[i] = a[i] + 10.0
> IDL> PRINT, a
> 0.00000 11.0000 12.0000 13.0000 4.00000
>
> This is the behavior I expect.
>
> IDL> a = FINDGEN(5)
> IDL> i = [2, 2, 2]
> IDL> a[i] = a[i] + 10.0
> IDL> PRINT, a
> 0.00000 1.00000 12.0000 3.00000 4.00000
>
> Why does it only do the operation *once* when
> IDL> HELP, a[i]
> <Expression> FLOAT = Array[3]
>
> IDL> a = FINDGEN(5)
> IDL> i = [2, 2, 2]
> IDL> a[i] = a[i] + [10.0, 10.0, 10.0]
> IDL> PRINT, a
> 0.00000 1.00000 12.0000 3.00000 4.00000

A fast solution (better than loops in many cases), is listed in the manual using
the histogram function (everyone's favorite), though it works for integer (long
or otherwise) types only.

I'll simply regurgitate:

The HISTOGRAM function can also be used to increment the elements of one vector
whose subscripts are contained in another vector. To increment those elements of
vector A indicated by vector B, use the command:

A = HISTOGRAM(B, INPUT=A, MIN=0, MAX=N_ELEMENTS(A)-1)

This method works for duplicate subscripts, whereas the following statement
never adds more than 1 to any element, even if that element is duplicated in
vector B:

A[B] = A[B]+1

JD

--
J.D. Smith |*| WORK: (607) 255-5842
Cornell University Dept. of Astronomy |*| (607) 255-6263
304 Space Sciences Bldg. |*| FAX: (607) 255-5875
Ithaca, NY 14853 |*|
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Radon Transform
Next Topic: Re: IDL and 3D Image

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

Current Time: Sat Oct 11 07:12:35 PDT 2025

Total time taken to generate the page: 1.43951 seconds