I ran across an interesting challenge recently, and I was thinking this community would know how to optimize it.
I have several data sets which have (currently) about 50k-100k points. The sets contain sequences of consecutive positive and negative real numbers, and I'm interested in finding the position where the sequences of negative numbers begin and end (or where the positive sequences begin and end; it is the same problem).
In the near future, these sets will be comes 500k points, or 1000k +. This isn't a struggle for a modern PC but I'd like to write efficient code for beauty's sake anyway.
Being a novice, I wrote this, which works well thanks to the "vanishing function":
testlist // Dimensions
32827
Table[
If[
testlist[[i]]*testlist[[i + 1]] < 0,
i,
## &[]
]
, {i, 1, Length[testlist] - 1}]
But, realizing that Table
is typically the slowest way to do something in Mathematica, I wrote this, using the same logic operations:
negPos[list_] :=
If[list[[#]]*list[[# + 1]] < 0, #, ## &[]] & /@ Range[Length[list] - 1]
Both achieve a similar processing time:
Timing[
Table[
If[testlist[[i]]*testlist[[i + 1]] < 0, i, ## &[]],
{i, 1, Length[testlist] - 1}];]
{0.171875, Null}
Timing[negPos[testlist];]
{0.140625, Null}
But, considering that both run in O[n] time, I would think they could be much faster. I'm too new to Mathematica to know how to optimize either the logic test or memory usage. Does someone know how to optimize this problem?