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

Home » Public Forums » archive » Re: Sum of elements in an array
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: Sum of elements in an array [message #2305] Thu, 09 June 1994 12:06
velt is currently offline  velt
Messages: 19
Registered: June 1994
Junior Member
In article ftb@due.uninett.no, ragnar@kvark.fi.uib.no (Ragnar Aas) writes:
> I'm working on a project where I need to sum the result of many IF-statements.
> In order to make this operation faster, I want to vectorize it. The way I
> want it is :
>
> sum=(array_1.element EQ 1)
>
> with sum a scalar, and array_1 an array. (BIG surprise!)
> In other words : If array_1.element equals 1, then add 1 to sum.
> How do I go about to solve this?
>
> Thanx in advance.
>
> Ragnar Aas

Try:

sum=n_elements(where(array_1.element EQ 1))

Or, when the values of element can only be 0 or 1:

sum=total(array_1.element)

Good Luck.

Robert Velthuizen
Dept of Radiology
University of South Florida.
Re: Sum of elements in an array [message #2324 is a reply to message #2305] Thu, 09 June 1994 05:17 Go to previous message
steinhh is currently offline  steinhh
Messages: 260
Registered: June 1994
Senior Member
In article <2t6tb9$ftb@due.uninett.no>, ragnar@kvark.fi.uib.no (Ragnar Aas) writes:
|> I'm working on a project where I need to sum the result of many IF-statements.
|> In order to make this operation faster, I want to vectorize it. The way I
|> want it is :
|>
|> sum=(array_1.element EQ 1)
|>
|> with sum a scalar, and array_1 an array. (BIG surprise!)
|> In other words : If array_1.element equals 1, then add 1 to sum.
|> How do I go about to solve this?
|>

I think sum = total(array_1 eq 1) does the trick.
In general, a logical operation on an array results in an array of
(BYTE) logical values, with 0 for false, and 1 for true.
TOTAL() adds up to a float, so no overflow.

|> Thanx in advance.
|>
|> Ragnar Aas

Stein Vidar
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to I get IDL postscript files from Sun to Mac?
Next Topic: How to contact Visual Numerics?

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

Current Time: Wed Oct 08 19:18:26 PDT 2025

Total time taken to generate the page: 0.00479 seconds