Re: Array subscript question [message #19203 is a reply to message #19199] |
Fri, 03 March 2000 00:00   |
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 |*|
|
|
|