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?
|