Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Idl-pvware > Re: fastest way...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 10 Topic 5618 of 6250
Post > Topic >>

Re: fastest way to find the first non-zero value in an array

by Spon <christoph.blau@[EMAIL PROTECTED] > Apr 8, 2008 at 06:03 AM

On Apr 8, 9:04 am, smas...@[EMAIL PROTECTED]
 wrote:
> Hi,
>
> I want to find the first non-zero value of an array. Is there a faster
> way to do this than with the "where" command: (where(array ne 0))[0]
> "Where" will look for all non-zero values and I only need the first
> one. It would be great if I could stop "where" in its search process
> as soon as it found one element...
>
> sebastien

Hi Sebastien,

I can think of two separate ways of going about it:

Firstly, for certain arrays it may be perfectly sensible to use the
'inefficient', FORTRAN 101 route:
i = 0
WHILE Array[i] EQ 0 DO i++
RETURN, i

Secondly, I've attempted to do it using HISTOGRAM below.
-----
 ; Is the first element non-zero?
 ; If it is, we can save ourselves a lot of
 ; hassle...
IF Array[0] NE 0 THEN RETURN, 0L

 ; We now know element 0 contains data = 0.
 ; Generate a histogram of the array such
 ; that this element will always be put in the
 ; first bin:
H = HISTOGRAM(CEIL(ABS(Array)), REVERSE_INDICES = RI)

 ; Are there non-zero elements in the array?
IF N_ELEMENTS(H) EQ 1 THEN MESSAGE, $
  'Array contains only zeroes!'

 ; Array[0] is always going to be in the first bin.
 ; Get the contents of that bin:
BinContents = RI[RI[0]:RI[1]-1]

 ; How many drops in that bin?
NBC = N_ELEMENTS(BinContents)

 ; Where do the drop indices stop increasing linearly?
 ; That's where the first non-zero element must be.
Index = WHERE((LINDGEN(NBC) - BinContents) NE 0, Count)

 ; If all the zeroes in the array come before the first
 ; non-zero value, then we won't get any indices returned,
 ; but the next index will be the next element after the
 ; end of our BinContents vector. This must be non-zero.
IF Count EQ 0 THEN RETURN, NBC

 ; Otherwise, use the WHERE results to return
 ; the index of first non-zero element of the array.
RETURN, Index[0]
-----

As far as I can tell, both methods work; and which is going to be
faster (between these two and just using WHERE) is going to depend on
your array.

Let us know how you get on,
Regards,

Chris
 




 10 Posts in Topic:
fastest way to find the first non-zero value in an array
smasson@[EMAIL PROTECTED]  2008-04-08 01:04:48 
Re: fastest way to find the first non-zero value in an array
"Clemens" <y  2008-04-08 10:42:50 
Re: fastest way to find the first non-zero value in an array
Spon <christoph.blau@[  2008-04-08 06:03:51 
Re: fastest way to find the first non-zero value in an array
Vince Hradil <hradilv@  2008-04-08 06:15:22 
Re: fastest way to find the first non-zero value in an array
David Fanning <news@[E  2008-04-08 07:34:38 
Re: fastest way to find the first non-zero value in an array
Spon <christoph.blau@[  2008-04-08 06:56:33 
Re: fastest way to find the first non-zero value in an array
Vince Hradil <hradilv@  2008-04-08 07:02:38 
Re: fastest way to find the first non-zero value in an array
=?ISO-8859-2?Q?F=D6LDY_La  2008-04-08 16:23:04 
Re: fastest way to find the first non-zero value in an array
Rick Towler <rick.towl  2008-04-08 11:41:10 
Re: fastest way to find the first non-zero value in an array
karo03de <karo03de@[EM  2008-04-09 13:02:32 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 12:17:15 CDT 2008.