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

Home » Public Forums » archive » N_ELEMENTS gt 0
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
N_ELEMENTS gt 0 [message #58676] Thu, 14 February 2008 01:37 Go to next message
chloesharrocks is currently offline  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 Go to previous message
Jean H. is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: print and precision
Next Topic: keyboard shortcut in IDL 7

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

Current Time: Fri Oct 10 13:22:30 PDT 2025

Total time taken to generate the page: 0.56104 seconds