I have a list of numbers and I am looking for a way to most efficiently find positions of values that satisfy a certain condition and group those positions that are sequential. To illustrate, a simple list can look like
x={1,2,4,6,2,3,4,5,7,9,1,3,4,1}
and I am interested in all numbers that are greater than 2 and so I expect the return to look like:
res={{3,4},{6,7,8,9,10},{12,13}}
Alternatively, I would also need the result being the values in x
however grouped in the same manner.
The stress is also on effectivness, this will be applied on a number of 100 MB data files.
My initial idea was:
Position[x, n_ /; n > 50] // Flatten
and then somehow go through the output and determined if the next position is previous plus 1 but I think this is not very effective (also I have no idea how to do it without complex loops).
Split
to do the last step, or fancier stuff from this question. $\endgroup$Split[Flatten@Position[list, n_ /; n > 2], #2 - #1 == 1 &]
$\endgroup$