Re: Odd behaviour in array indexing ? [message #34179] |
Fri, 21 February 2003 09:29 |
Liam E. Gumley
Messages: 378 Registered: January 2000
|
Senior Member |
|
|
"mwvogel" <mvogel@rdiag.fgg.eur.nl> wrote in message
news:b35fla$q50$2@mrelay2.eur.nl...
> Today I realized something is amiss in IDL
> When I do
> index = [1,0,2,3,1,2,3,4]
> m = FLTARR(8)
> d = FINDGEN(8)
> m[index] = d
> print, m
> 1.00000 4.00000 5.00000 6.00000 7.00000
> 0.000000 0.000000 0.000000
>
> Now I would have assumed that IDL would automatically *add* the numbers
with
> identical indices. Not doing so is a potential performance penaly, right ?
Or am I mistaken....
This subject is discussed in the "Vectorization question" thread associated
with my name at Google Groups:
http://groups.google.com/groups?group=comp.lang.idl-pvwave
Cheers,
Liam Gumley.
Practical IDL Programming
http://www.gumley.com/
|
|
|
Re: Odd behaviour in array indexing ? [message #34182 is a reply to message #34179] |
Fri, 21 February 2003 07:48  |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
mwvogel wrote:
>
> Today I realized something is amiss in IDL
> When I do
> index = [1,0,2,3,1,2,3,4]
> m = FLTARR(8)
> d = FINDGEN(8)
> m[index] = d
> print, m
> 1.00000 4.00000 5.00000 6.00000 7.00000
> 0.000000 0.000000 0.000000
>
> Now I would have assumed that IDL would automatically *add* the numbers with
> identical indices. ...
I'm not sure why you would assume that.
> ... Not doing
> so is a potential performance penaly, right ?
Actually, doing it would incur a performance penalty. The way it's
actually implemented internally is equivalent to the following:
for i=0,7 do m[index[i]] = d[i]
except, of course, that it's far faster as "m[index] = d" than as an
explicit loop. That's a pretty efficient loop. I can't see anyway to
implement the behavior you want, that isn't a whole lot slower. The
closest I can get is equivalent to:
initialized = intarr(8)
FOR i=0,7 DO BEGIN
IF initialized[index[i]] THEN m[index[i]] = d[i];
ELSE m[index[i]] = m[index[i]] + d[i];
initialized[index[i]] = 1;
ENDIF
|
|
|
Re: Odd behaviour in array indexing ? [message #34184 is a reply to message #34182] |
Fri, 21 February 2003 07:53  |
tam
Messages: 48 Registered: February 2000
|
Member |
|
|
David Fanning wrote:
> mwvogel (mvogel@rdiag.fgg.eur.nl) writes:
>
>
>> Today I realized something is amiss in IDL
>> When I do
>> index = [1,0,2,3,1,2,3,4]
>> m = FLTARR(8)
>> d = FINDGEN(8)
>> m[index] = d
>> print, m
>> 1.00000 4.00000 5.00000 6.00000 7.00000
>> 0.000000 0.000000 0.000000
>>
>> Now I would have assumed that IDL would automatically *add* the numbers with
>> identical indices. Not doing
>> so is a potential performance penaly, right ? Or am I mistaken....
>
>
> I find it hard to say what exactly you are trying to
> do here, but IDL seems to be working exactly as I would
> expect it to. Variables on the left hand side of the
> expression are having things assigned to them. I'm not
> sure why you think the assignments should *add*. If I do
> this:
>
> a = intarr(2)
> a[1] = 5
>
> And later,
>
> a[1] = 6
>
> I sure don't want a[1] to equal 11. That is exactly
> what you seem to be asking for above.
>
> Cheers,
>
> David
>
Just to amplify a little. Even if you had written this as
m[index] = m[index] + d
where you might have a more realistic hope that
the m's would accumulate values, it wouldn't work.
It's best to think of IDL array operations as fully parallelized
operations, where there is a separate little CPU handling
the operation for each index. If you need dependencies
on prior iterations, then you need to do things in some different
fashion.
Beware, I think you are about to enter the 'Histogram Zone'.
Dee da dee da ....
Regards,
Tom McGlynn
|
|
|
Re: Odd behaviour in array indexing ? [message #34185 is a reply to message #34182] |
Fri, 21 February 2003 07:40  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
mwvogel (mvogel@rdiag.fgg.eur.nl) writes:
> Today I realized something is amiss in IDL
> When I do
> index = [1,0,2,3,1,2,3,4]
> m = FLTARR(8)
> d = FINDGEN(8)
> m[index] = d
> print, m
> 1.00000 4.00000 5.00000 6.00000 7.00000
> 0.000000 0.000000 0.000000
>
> Now I would have assumed that IDL would automatically *add* the numbers with
> identical indices. Not doing
> so is a potential performance penaly, right ? Or am I mistaken....
I find it hard to say what exactly you are trying to
do here, but IDL seems to be working exactly as I would
expect it to. Variables on the left hand side of the
expression are having things assigned to them. I'm not
sure why you think the assignments should *add*. If I do
this:
a = intarr(2)
a[1] = 5
And later,
a[1] = 6
I sure don't want a[1] to equal 11. That is exactly
what you seem to be asking for above.
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|