MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: FindLast NZ in list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg46218] Re: FindLast NZ in list
  • From: "Ted Ersek" <ersekjm at tqci.net>
  • Date: Thu, 12 Feb 2004 07:15:45 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

I haven't yet answered this one.  First I make a list of data.

len=1000000;
data=Table[0,{len}];
Do[
    Part[data,Random[Integer,{1,len}]] = Random[],
    {Floor[len/100]}
];

-----------------------
This quickly finds the last non-zero element.

Select[Reverse[data],#=!=0&,1] //Timing

{0.125 Second,{0.447473}}

-------------------------
This quickly finds the position of the last non-zero element.

1+Length[data]-Position[Reverse[data],_?(#=!=0&),1,1,Heads->False] //Timing

   {0.109 Second,{{999854}}}

-------------------------
Note:   (#=!=0&)  is faster than (# != 0&) but they are not the same.
0.0 =!= 0  returns True.
0.0 != 0 returns False.

This should be faster when (data) is a packed array, but I wasn't able 
to demonstrate that. In this case we should have a packed array when all 
parts of the list are machine numbers with the same head.

----------------------------
Wolfram Research should have made it so we could do:
    Position[data, pattn, levelspec, -1 ]
    Select[data, test, -1]

I wrote a package that does that and much more.  The package is posted at 
http://library.wolfram.com/infocenter/MathSource/495/
While the package extends Position, Select and others vary well, it 
isn't as fast as my version above.  I might improve the package in the 
coming weeks.

---------------------------
Regards,
    Ted Ersek
Download Mathematica tips from
http://library.wolfram.com/infocenter/MathSource/4557/
or see them on a web site at 
http://www.verbeia.com/mathematica/tips/Tricks.html

 


  • Prev by Date: A new webMathematica application, called BiokmodWeb
  • Next by Date: Re: simplifying first-order diff eq solution
  • Previous by thread: A new webMathematica application, called BiokmodWeb
  • Next by thread: GroebnerBasis and Polynomial Routines