Skip to main content
added 64 characters in body
Source Link
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.

Select[Partition[lst, n + 1, 1, 1], 
    Abs[Take[#, -1] - Mean[Drop[#, -1]]][[1]] > p*StandardDeviation[Drop[#, -1]] &] 
    // First // Last

For a more efficient implementation - see Mr Wizard's answer

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.

Select[Partition[lst, n + 1, 1, 1], 
    Abs[Take[#, -1] - Mean[Drop[#, -1]]][[1]] > p*StandardDeviation[Drop[#, -1]] &] 
    // First // Last

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.

Select[Partition[lst, n + 1, 1, 1], 
    Abs[Take[#, -1] - Mean[Drop[#, -1]]][[1]] > p*StandardDeviation[Drop[#, -1]] &] 
    // First // Last

For a more efficient implementation - see Mr Wizard's answer

Fixed select
Source Link
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[lstSelect[Partition[lst, n + 1, 1, 1], 
    Abs[Take[#, -1] - Mean[Drop[#, -1]]][[1]] > p*StandardDeviation[Drop[#, -1]] &] 
    // First // Last

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

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.

Select[Partition[lst, n + 1, 1, 1], 
    Abs[Take[#, -1] - Mean[Drop[#, -1]]][[1]] > p*StandardDeviation[Drop[#, -1]] &] 
    // First // Last
Added select method
Source Link
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

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

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
Source Link
Jonie
  • 1.2k
  • 6
  • 14
Loading