Re: Position of Sign change in long list
- To: mathgroup at smc.vnet.net
- Subject: [mg18729] Re: Position of Sign change in long list
- From: "David Keith" <dkeith at hevanet.com>
- Date: Sat, 17 Jul 1999 02:36:46 -0400
- Organization: Hevanet Communications
- References: <7mjvfd$foi@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Martin, Here is one possibility: Since it uses Sign[], there are 3 flavors of sign: negative, zero, and positive. That could be fixed by inserting, for example, /.0->1 in the right spot. It checks a list of signs against such a list displaced by 1 for inequality, and so returns the first location where an element's sign disagrees with the sign of the element to its left. If none, it returns an error and a null list. -Dave PS. Did you have a famous grandfather? signChangePosition[d_] := Module[{s = Sign /@ d}, First[ Position[MapThread[#1 == #2 &, {Drop[s, 1], Drop[s, -1]}], False]][[1]] + 1] Martin Rommel wrote in message <7mjvfd$foi at smc.vnet.net>... >I have a long list of data points and need to find the position where the >sign changes for the first time. >My first attempt was > >test=Table[Sin[Pi i/5000. +1],{i,10000}]; > >With[{a=Abs[test]},Position[a,Min[a]]] > >which works, but is slow. It turns out that Min is relatively slow and >avoiding it can save time. The following is almost a factor 4 faster: > >With[{st=Sign[test]},Length[Split[st][[1]]]] > >If you can think of a still faster or more elegant solution please let me >know! > >Martin > > >