Re: 2 arrays, average, missing data [message #45411] |
Thu, 08 September 2005 10:31 |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
Benjamin Luethi wrote:
> An short answer would be:
>
> C = (A+B)/((A NE 0)+(B NE 0))
>
> The divisor is 1+1=2 if both A and B are not 0.
> 0+1=1 if one of them is 0.
> 0+0=0 if both A and B are 0.
>
> If A and B are integer, divison by zero produces 0, which is the wanted
> result.
> If A or B are of type double or float, the third case produces NaN.
> Convert it
> to zero using:
>
> sel = where(finite(C,/NaN),count)
> if count gt 0 then C[sel] = 0
Simpler:
C = (A+B)/((A NE 0)+(B NE 0) > 1.0)
|
|
|
Re: 2 arrays, average, missing data [message #45412 is a reply to message #45411] |
Thu, 08 September 2005 08:46  |
kimberlite
Messages: 3 Registered: January 2005
|
Junior Member |
|
|
Thanks so much for the replies! Some of the discussion is over my
head at this time, but I did get Bob's suggestion to work (I believe)
after changing the array to floating and specifying my array
dimensions.
I like the shorter options put forward by Ben and Craig, too! I must
do some more reading about the NaN.
Thanks again! (better than my alternative brute force approach that
was gonna start today!)
- K
|
|
|
Re: 2 arrays, average, missing data [message #45413 is a reply to message #45412] |
Thu, 08 September 2005 08:24  |
Benjamin Luethi
Messages: 22 Registered: December 2004
|
Junior Member |
|
|
An short answer would be:
C = (A+B)/((A NE 0)+(B NE 0))
The divisor is 1+1=2 if both A and B are not 0.
0+1=1 if one of them is 0.
0+0=0 if both A and B are 0.
If A and B are integer, divison by zero produces 0, which is the wanted
result.
If A or B are of type double or float, the third case produces NaN.
Convert it
to zero using:
sel = where(finite(C,/NaN),count)
if count gt 0 then C[sel] = 0
Ben
On Thu, 08 Sep 2005 16:18:44 +0200, KJM <kimberlite@gmail.com> wrote:
> HI All,
> IDL newbie here, I would appreciate any help. (Have been pouring over
> Gumley and Fanning books for a day now, can't get this simple
> calculation done.)
>
> I have 2 arrays. Each array has float values and missing
> data(value=0). I want to create a third array that has the average of
> the two arrays if there are two good values. Otherwise, I want the
> third array to take the value of the array that has data.
>
>
> If my arrays are A and B, and the new array C, I know I can use:
>
> C = (A+B)/2
>
> to get the third array with averaged values. The only problem, is
> that missing data values are averaged in with good values also. (ie:
> 275 averaged w/ 0 -- when I want to just take the 275 value).
>
> Have tried If statements, -- but I realize these are all for scalar
> values, not array.
>
> My basic idea is:
>
> If A = 0, C = B
> If B = 0, C = A
> If A and B EQ 0, C = 0
> If A and B NE 0, C = (A+B)/2
>
>
>
>
> Any help appreciated!
> Thanks.
>
--
-------------+
|
|
|
Re: 2 arrays, average, missing data [message #45414 is a reply to message #45413] |
Thu, 08 September 2005 07:55  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
"KJM" <kimberlite@gmail.com> writes:
> HI All,
> IDL newbie here, I would appreciate any help. (Have been pouring over
> Gumley and Fanning books for a day now, can't get this simple
> calculation done.)
>
> I have 2 arrays. Each array has float values and missing
> data(value=0). I want to create a third array that has the average of
> the two arrays if there are two good values. Otherwise, I want the
> third array to take the value of the array that has data.
>
>
> If my arrays are A and B, and the new array C, I know I can use:
>
> C = (A+B)/2
>
> to get the third array with averaged values. The only problem, is
> that missing data values are averaged in with good values also. (ie:
> 275 averaged w/ 0 -- when I want to just take the 275 value).
Here's something without WHERE's
MISSING = 0.0
C = (A+B)/((A NE MISSING) + (B NE MISSING))
You'll get NaN whereever both values are missing. This is easily
extendible to the case where you have N arrays with M values each.
Just arrange them into an MxN array,
DATA = DBLARR(M,N)
... fill data values ...
C = TOTAL(DATA,2)/TOTAL(DATA NE MISSING,2)
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: 2 arrays, average, missing data [message #45415 is a reply to message #45414] |
Thu, 08 September 2005 07:53  |
R.G. Stockwell
Messages: 363 Registered: July 1999
|
Senior Member |
|
|
"KJM" <kimberlite@gmail.com> wrote in message
news:1126189124.079832.197820@f14g2000cwb.googlegroups.com.. .
> HI All,
...
> If A = 0, C = B
> If B = 0, C = A
> If A and B EQ 0, C = 0
> If A and B NE 0, C = (A+B)/2
> Any help appreciated!
> Thanks.
Hi,
something along the lines of
gooddata = a*b
c = dblarr(n_elements(a))
zeros = where(gooddata eq 0,count,complement = nonzeros)
if count gt 0 then begin
c[nonzeros] = (a[nonzeros]+b[nonzeros])/2
c[zeros] = a[zeros] > b[zeros]
endif
NOTE: this assumes the values are greater than zero.
If they are not, then using zeros as the "bad value" may cause some
problems.
Cheers,
bob
|
|
|
Re: 2 arrays, average, missing data [message #45417 is a reply to message #45415] |
Thu, 08 September 2005 07:38  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
KJM writes:
> IDL newbie here, I would appreciate any help. (Have been pouring over
> Gumley and Fanning books for a day now, can't get this simple
> calculation done.)
>
> I have 2 arrays. Each array has float values and missing
> data(value=0). I want to create a third array that has the average of
> the two arrays if there are two good values. Otherwise, I want the
> third array to take the value of the array that has data.
>
>
> If my arrays are A and B, and the new array C, I know I can use:
>
> C = (A+B)/2
>
> to get the third array with averaged values. The only problem, is
> that missing data values are averaged in with good values also. (ie:
> 275 averaged w/ 0 -- when I want to just take the 275 value).
>
> Have tried If statements, -- but I realize these are all for scalar
> values, not array.
>
> My basic idea is:
>
> If A = 0, C = B
> If B = 0, C = A
> If A and B EQ 0, C = 0
> If A and B NE 0, C = (A+B)/2
I think your basic idea is great. :-)
Here is what I would do.
C = (A + B) / 2
indices = WHERE(A EQ 0 AND B NE 0, count)
IF count GT 0 THEN C[indices] = B
indices = WHERE(A NE 0 AND B EQ 0, count)
IF count GT 0 THEN C[indices] = A
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|