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

Home » Public Forums » archive » Scary bug with array assignment
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Scary bug with array assignment [message #89257] Sun, 10 August 2014 11:57 Go to previous message
Paddy Leahy is currently offline  Paddy Leahy
Messages: 7
Registered: August 2013
Junior Member
Hi,
I'm running IDL 8.2. I'm accumulating arrays stored as separate
fields of a structure into a multidimensional array, in order to average it.
I'm using the recommended notation of array assignment by first index, like this

sum = FLTARR(npix,3)
FOR ifile = 0,9 DO BEGIN
< read data from file into data array>
FOR jj=0,3 sum[0,jj] += data.(jj)
ENDFOR

On the first iteration, each row of sum is set to the data in data, as expected. But on subsequent iterations, the *FIRST* element of data.(jj) is added to *EACH*
element of the jjth row of sum.

It turns out that the use of structures is not required to trigger this problem.
Here are some actual results cut and pasted from the command line:

HIDL> test = fltarr(3,3)
HIDL> sum = fltarr(3,3)
HIDL> data = [[-1,0,1],[-2,0,2],[-3,0,3]]
HIDL> print, sum
0.00000 0.00000 0.00000
0.00000 0.00000 0.00000
0.00000 0.00000 0.00000
HIDL> print, data
-1 0 1
-2 0 2
-3 0 3
HIDL> for jj=0,2 do sum[0,jj] += data[*,jj]
HIDL> print, sum
-1.00000 0.00000 1.00000
-2.00000 0.00000 2.00000
-3.00000 0.00000 3.00000
HIDL> for jj=0,2 do sum[0,jj] += data[*,jj]
HIDL> print, sum
-2.00000 -1.00000 0.00000
-4.00000 -2.00000 0.00000
-6.00000 -3.00000 0.00000
HIDL> sum[*] = 0
HIDL> for jj=0,2 do sum[*,jj] += data[*,jj]
HIDL> for jj=0,2 do sum[*,jj] += data[*,jj]
HIDL> print, sum
-2.00000 0.00000 2.00000
-4.00000 0.00000 4.00000
-6.00000 0.00000 6.00000

As I understand it, the two notations sum[*,jj] = data[*,jj] and sum[0,jj] = data[*,jj] should be equivalent (assuming the row length is the same in the two arrays, as above), although the second is supposed to be faster.
But as you see, the first notation gives bizarre results.

Is there a big hole in my understanding of IDL array assignment, or is this a ghastly bug?
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: cgContour with different clipping
Next Topic: How to assign command line arguments to variables in another class

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

Current Time: Wed Oct 08 20:09:04 PDT 2025

Total time taken to generate the page: 0.78539 seconds