Re: First nonzero in list
- To: mathgroup at smc.vnet.net
- Subject: [mg110856] Re: First nonzero in list
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Thu, 8 Jul 2010 20:33:27 -0400 (EDT)
- References: <i13tq3$7gd$1@smc.vnet.net>
"S. B. Gray" wrote:
> I have lists such as
>
> th = {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501,
> 462, 402, 325, 254, 189, 99, 81, 36, 9, 8, 3, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
>
> and I want to find the position of the last nonzero element (e.g. 3 at
> position 25). Trivial, but some poking around did not reveal a neat way.
>
> Anyone? Thank you.
>
> Steve Gray
>
For linear lists at least, this seems to work:
Extract[th, Last@Position[th, x_ /; x != 0]]
--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de