Adding arrays [message #91465] |
Fri, 17 July 2015 00:38  |
Dete van Eeden
Messages: 32 Registered: July 2015
|
Member |
|
|
Hallo,
I am trying to add arrays together in a for loop but the end result is just equal to the last array.
for k=0,5 do begin
read in the array etc.
final = final + array
endfor
The final is suppose to be the sum of all the arrays.
Thank you!
|
|
|
|
|
Re: Adding arrays [message #91473 is a reply to message #91471] |
Fri, 17 July 2015 11:26   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Dete van Eeden writes:
> How do I define an array that consists of 5 arrays each with 256x256 elements?
>
> For example final(k) is the sum of the arrays from 1 to 5 and final=fltarr(256,256)
>
> It seems so simple but I cant get it right :-)
total = FltArr(256,256)
data = FltArr(256, 256, 5)
FOR j=0,4 do total = Temporary(total) + data[*,*,j]
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding arrays [message #91474 is a reply to message #91473] |
Fri, 17 July 2015 11:36   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
>
> Dete van Eeden writes:
>
>> How do I define an array that consists of 5 arrays each with 256x256 elements?
>>
>> For example final(k) is the sum of the arrays from 1 to 5 and final=fltarr(256,256)
>>
>> It seems so simple but I cant get it right :-)
>
> total = FltArr(256,256)
> data = FltArr(256, 256, 5)
> FOR j=0,4 do total = Temporary(total) + data[*,*,j]
Or, the IDL Way:
data = Randomu(seed, 256,256,5)
total = Total(data, 3)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
|
Re: Adding arrays [message #91481 is a reply to message #91480] |
Mon, 20 July 2015 00:34  |
dete.liebenberg
Messages: 1 Registered: July 2015
|
Junior Member |
|
|
On Sunday, July 19, 2015 at 2:34:32 PM UTC+2, Dete van Eeden wrote:
> Thank you very much :-)
Is there a reason why this would work for 3 dimensions but not 2.
If a have data=fltarr(256,4)
the total is equal to the last array again?
Thank you
|
|
|