Re: Array subscript question [message #19199] |
Fri, 03 March 2000 00:00 |
John-David T. Smith
Messages: 384 Registered: January 2000
|
Senior Member |
|
|
"J.D. Smith" wrote:
>
> "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.
Actually, that's not really true. The sun is out and temporarily incapacitated
by reasoning abilities.
As long as your i vector is >=0 and <n_elements(A), you can do:
a=a+histogram(i,MIN=0,MAX=n_elements(a)-1)*10.
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 |*|
|
|
|
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 |*|
|
|
|
Re: Array subscript question [message #19222 is a reply to message #19199] |
Thu, 02 March 2000 00:00  |
Steve Hartmann
Messages: 21 Registered: March 2000
|
Junior 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
>
> Even this doesn't help.
>
> Ken
>
>
The expression on the right is evaluated first. So your last example,
a[i] = a[i] + [10.0, 10.0, 10.0]
could be written as,
[ a[2], a[2], a[2] ] = [ a[2], a[2], a[2] ] + [10.0, 10.0, 10.0]
or,
[ a[2], a[2], a[2] ] = [ 12.0, 12.0, 12.0 ]
which is the same as,
a[2] = 12.0
a[2] = 12.0
a[2] = 12.0
So the operation does occur more than once, but it's just the same
operation. If you try a[i] = a[i] + [x, y, z], a[2] will equal 'z', since
that expression was executed last.
I hope this helps,
Steve Hartmann
|
|
|
Re: Array subscript question [message #19223 is a reply to message #19222] |
Thu, 02 March 2000 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Kenneth P. Bowman (bowman@null.edu) writes:
> 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]
Uh, I'm sure it's doing the operation three times, Ken.
But always to the same value. :-)
Sounds what you want is a loop. I'm not too fond
of them in IDL, but certainly it's appropriate here.
I think the answer to your question really is this:
array subscripting operations are NOT loops!
FOR I=0,2 DO a[2] = a[2] + 10.0
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|