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

Home » Public Forums » archive » Re: 2 arrays, average, missing data
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: 2 arrays, average, missing data [message #45411] Thu, 08 September 2005 10:31
James Kuyper is currently offline  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 Go to previous message
kimberlite is currently offline  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 Go to previous message
Benjamin Luethi is currently offline  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 Go to previous message
Craig Markwardt is currently offline  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 Go to previous message
R.G. Stockwell is currently offline  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 Go to previous message
David Fanning is currently offline  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/
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Ordered index array
Next Topic: Re: Postscript colour sampler?

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

Current Time: Fri Oct 10 11:12:25 PDT 2025

Total time taken to generate the page: 0.44078 seconds