Initialize new variable array with nan or -9999 values [message #87636] |
Thu, 20 February 2014 10:31  |
morganlsilverman
Messages: 46 Registered: February 2013
|
Member |
|
|
Hello,
This seems like it should be so simple but I can't figure out how to do it. I am creating a new array, distance=fltarr(304,336,500) and I want it to be initially filled with either nan or -9999. My results could possibly be 0.0 so I need to know where those are calculated values and not just part of the original array. Thank you.
Sincerely,
Morgan
|
|
|
|
Re: Initialize new variable array with nan or -9999 values [message #87638 is a reply to message #87636] |
Thu, 20 February 2014 10:36   |
John Correira
Messages: 25 Registered: August 2011
|
Junior Member |
|
|
On 02/20/2014 01:31 PM, Morgan Silverman wrote:
> Hello,
>
> This seems like it should be so simple but I can't figure out how to
> do it. I am creating a new array, distance=fltarr(304,336,500) and I
> want it to be initially filled with either nan or -9999. My results
> could possibly be 0.0 so I need to know where those are calculated
> values and not just part of the original array. Thank you.
>
> Sincerely, Morgan
>
Either
distance = fltarr(304,336,500,/NOZERO)
distance[*] = !values.f_nan
or
distance = make_array(304,336,500,/FLOAT,VALUE=!values.f_nan)
John
|
|
|
Re: Initialize new variable array with nan or -9999 values [message #87639 is a reply to message #87637] |
Thu, 20 February 2014 10:37   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> distance=fltarr(304,336,500) + -9999
Whoops! Got away from me!
distance=fltarr(304,336,500) - 9999
Or,
distance=fltarr(304,336,500) + !Values.F_NaN
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: Initialize new variable array with nan or -9999 values [message #87643 is a reply to message #87639] |
Thu, 20 February 2014 13:11   |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Thursday, February 20, 2014 11:37:22 AM UTC-7, David Fanning wrote:
> David Fanning writes:
>
>
>
>> distance=fltarr(304,336,500) + -9999
>
>
>
> Whoops! Got away from me!
>
>
>
> distance=fltarr(304,336,500) - 9999
>
>
>
> Or,
>
>
>
> distance=fltarr(304,336,500) + !Values.F_NaN
>
>
>
> 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.")
Also take a look at REPLICATE.
-Chris
|
|
|
|
Re: Initialize new variable array with nan or -9999 values [message #87645 is a reply to message #87638] |
Thu, 20 February 2014 13:33   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
On 02/20/14 13:36, John Correira wrote:
> On 02/20/2014 01:31 PM, Morgan Silverman wrote:
>> Hello,
>>
>> This seems like it should be so simple but I can't figure out how to
>> do it. I am creating a new array, distance=fltarr(304,336,500) and I
>> want it to be initially filled with either nan or -9999. My results
>> could possibly be 0.0 so I need to know where those are calculated
>> values and not just part of the original array. Thank you.
>>
>> Sincerely, Morgan
>>
>
> Either
>
> distance = fltarr(304,336,500,/NOZERO)
> distance[*] = !values.f_nan
>
> or
>
> distance = make_array(304,336,500,/FLOAT,VALUE=!values.f_nan)
+1 on the MAKE_ARRAY for this purpose.
It's more self-documenty (IMO).
|
|
|
|
|
Re: Initialize new variable array with nan or -9999 values [message #87648 is a reply to message #87645] |
Thu, 20 February 2014 14:14   |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
On Thursday, February 20, 2014 4:33:01 PM UTC-5, Paul van Delst wrote:
> On 02/20/14 13:36, John Correira wrote:
For large arrays, there is a speed advantage to using REPLICATE or MAKE_ARRAY in which the array is initialized with the correct value, as opposed to
fltarr(304,336,500) - 9999
in which the array is first initialized with zeros, and then 9999 is subtracted from each element. Most computers should be able easily handle a 50 Mb array, so in this case the speed difference would be a fraction of a second. But it is something to keep in mind when arrays are large enough that a factor of two speed improvement matters. --Wayne
|
|
|
Re: Initialize new variable array with nan or -9999 values [message #87653 is a reply to message #87648] |
Thu, 20 February 2014 17:36   |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
On Thu, 20 Feb 2014 14:14:54 -0800 (PST), wlandsman wrote:
> For large arrays, there is a speed advantage to using REPLICATE or MAKE_ARRAY in which the array is initialized with the correct value, as opposed to
>
> fltarr(304,336,500) - 9999
>
> in which the array is first initialized with zeros, and then 9999 is subtracted from each element.
Yes, one should think so. But it is not true. "fltarr()-9999" ist
faster than make_array():
IDL> t0=systime(1) &for i=0,9 do a=fltarr(1000,1000,100)-9999.
&print,systime(1)-t0 &help,a
1.8280001
A FLOAT = Array[1000, 1000, 100]
IDL> t0=systime(1) &for i=0,9 do
a=make_array(1000,1000,100,value=-9999.) &print,systime(1)-t0 &help,a
2.7969999
A FLOAT = Array[1000, 1000, 100]
IDL> print,!version
{ x86 Win32 Windows Microsoft Windows 8.0.1 Oct 5 2010 32
64}
Don't ask me why.
Cheers, Heinz
|
|
|
Re: Initialize new variable array with nan or -9999 values [message #87654 is a reply to message #87653] |
Thu, 20 February 2014 20:11   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Heinz Stege writes:
> Yes, one should think so. But it is not true. "fltarr()-9999" ist
> faster than make_array():
>
> IDL> t0=systime(1) &for i=0,9 do a=fltarr(1000,1000,100)-9999.
> &print,systime(1)-t0 &help,a
> 1.8280001
> A FLOAT = Array[1000, 1000, 100]
> IDL> t0=systime(1) &for i=0,9 do
> a=make_array(1000,1000,100,value=-9999.) &print,systime(1)-t0 &help,a
> 2.7969999
> A FLOAT = Array[1000, 1000, 100]
> IDL> print,!version
> { x86 Win32 Windows Microsoft Windows 8.0.1 Oct 5 2010 32 64}
Oh, don't you just hate those guys who use their cell phones to fact
check everything you say! ;-)
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: Initialize new variable array with nan or -9999 values [message #87663 is a reply to message #87653] |
Fri, 21 February 2014 06:47   |
John Correira
Messages: 25 Registered: August 2011
|
Junior Member |
|
|
On 02/20/2014 08:36 PM, Heinz Stege wrote:
> IDL> t0=systime(1) &for i=0,9 do a=fltarr(1000,1000,100)-9999.
> &print,systime(1)-t0 &help,a
> 1.8280001
> A FLOAT = Array[1000, 1000, 100]
> IDL> t0=systime(1) &for i=0,9 do
> a=make_array(1000,1000,100,value=-9999.) &print,systime(1)-t0 &help,a
> 2.7969999
> A FLOAT = Array[1000, 1000, 100]
> IDL> print,!version
> { x86 Win32 Windows Microsoft Windows 8.0.1 Oct 5 2010 32
> 64}
MAKE_ARRAY is faster on my machine:
IDL> tic
IDL> for i=0,9 do a=fltarr(1000,1000,100)-9999.
IDL> toc
IDL> help, a
Elapsed time is 3.018326s
A FLOAT = Array[1000, 1000, 100]
IDL> tic
IDL> for i=0,9 do a=make_array(1000,1000,100,value=-9999.)
IDL> toc
IDL> help, a
Elapsed time is 2.075103s
A FLOAT = Array[1000, 1000, 100]
IDL> print,!version
{ x86_64 linux unix linux 8.2 Apr 10 2012 64
64}
|
|
|
Re: Initialize new variable array with nan or -9999 values [message #87664 is a reply to message #87663] |
Fri, 21 February 2014 06:58  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
John Correira writes:
> MAKE_ARRAY is faster on my machine:
>
> IDL> tic
> IDL> for i=0,9 do a=fltarr(1000,1000,100)-9999.
> IDL> toc
> IDL> help, a
>
> Elapsed time is 3.018326s
> A FLOAT = Array[1000, 1000, 100]
>
> IDL> tic
> IDL> for i=0,9 do a=make_array(1000,1000,100,value=-9999.)
> IDL> toc
> IDL> help, a
>
> Elapsed time is 2.075103s
> A FLOAT = Array[1000, 1000, 100]
>
> IDL> print,!version
> { x86_64 linux unix linux 8.2 Apr 10 2012 64
> 64}
I think you have to be a little careful how you do this experiment. But,
starting IDL from scratch each time (.reset) and then running the
Make_Array method or the Add-To method, I get these results on my
Windows 64-bit machine with IDL 8.2.3:
Make_Array Method: 1.572
Add-To Method: 1.811
Maybe 15% faster. I don't think I would be losing sleep over it. :-)
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.")
|
|
|