Re: Sum of elements in an array [message #2305] |
Thu, 09 June 1994 12:06 |
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  |
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
|
|
|