N_ELEMENTS gt 0 [message #58676] |
Thu, 14 February 2008 01:37  |
chloesharrocks
Messages: 16 Registered: January 2008
|
Junior Member |
|
|
Is it possible to combine the N_ELEMENTS function with the greater
than operator? I just want to search through an array and find out
how many elements exist which are greater than zero (I don't need to
know where they are, just how many), but I can't get it to work. I
have an array called good_index which I happen to know has 6 values >
zero, but when I run the following code, it tells me I only have 1
value > zero:
number = (N_ELEMENTS(good_index) gt 0)
print, number
Is there a way to do this without using a where function?
Thanks
Chloé
|
|
|
Re: N_ELEMENTS gt 0 [message #58758 is a reply to message #58676] |
Thu, 14 February 2008 10:12  |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
chloesharrocks@gmail.com wrote:
> Is it possible to combine the N_ELEMENTS function with the greater
> than operator? I just want to search through an array and find out
> how many elements exist which are greater than zero (I don't need to
> know where they are, just how many), but I can't get it to work. I
> have an array called good_index which I happen to know has 6 values >
> zero, but when I run the following code, it tells me I only have 1
> value > zero:
>
> number = (N_ELEMENTS(good_index) gt 0)
> print, number
>
> Is there a way to do this without using a where function?
> Thanks
> Chlo�
What you get is normal. N_elements(X) always returns 1 value, the number
of elements of X... so your statements returns 1 if good_index has at
least one element, and 0 if it is undefined.
Now, the easiest way is:
useless = where(good_index gt 0, countPositiveEntries)
useless = 0
print, countPositiveEntries
if you don't want that, you can use "total"
countPositiveEntries = total(good_index gt 0)
Note that you may have to use data[good_index]... it makes more sense,
as anyway you can't really have negative index (technically, you can,
but they make no sense!).
Jean
|
|
|