MathGroup Archive 1999

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

Search the Archive

Re: Position of Sign change in long list


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
>
>
>




  • Prev by Date: How can I plot the region satisfying an inequality?
  • Next by Date: Re: Rudimentary Spell Checker
  • Previous by thread: Re: Position of Sign change in long list
  • Next by thread: V4.0 Compatibility (Financial Derivatives book)