Skip to main content
2 of 4
Added select method
Jonie
  • 1.2k
  • 6
  • 14

Here's an attempt (without select)

lst = {1, 2, 2, 1, 2, 5, 2, 4};
n = 4;
p = 1;
Flatten[If[Abs[Take[#, -1] - Mean[Drop[#, -1]]][[1]] > p *StandardDeviation[Drop[#, -1]],
 Take[#, -1], {}] & /@ Partition[lst, n + 1, 1, 1]] // First

Alternatively with Select.

Flatten@Select[Partition[lst, n + 1, 1, 1], 
   Abs[Take[#, -1] - Mean[Drop[#, -1]]][[1]] > p*StandardDeviation[Drop[#, -1]] &] // Last
Jonie
  • 1.2k
  • 6
  • 14