Is there a easy way to determine the local min and maxes of an array of values. For example
Element Value Note
1 1
2 3
3 5
4 6
5 7 max
5 5
6 4 min
7 6
8 9
9 10 max
10 8
11 7
12 5 min
13 10
so an array that is defined like:
let arr = [|1;3;5;6;7;5;4;6;9;10;8;7;5;10|]
would identify
mins = [|4;5|]
and
maxs = [|7;10|]
It could be a list or Sequence as well as an array. Two questions
Thx
This looks like a job for... Seq.windowed! <cue superhero music>
let arr = [|1;3;5;6;7;5;4;6;9;10;8;7;5;10|]
let _,mins,maxs =
arr |> Seq.windowed 3 |> Seq.fold (fun (i,mins,maxs) [|a;b;c|] ->
if a>b&&b<c then (i+1, i::mins, maxs)
elif a<b&&b>c then (i+1, mins, i::maxs)
else (i+1, mins, maxs)) (1,[],[])
arr |> Seq.iteri (fun i x -> printfn "%2d: %2d" i x)
printfn "mins %A" mins
printfn "maxs %A" maxs
(*
0: 1
1: 3
2: 5
3: 6
4: 7
5: 5
6: 4
7: 6
8: 9
9: 10
10: 8
11: 7
12: 5
13: 10
mins [12; 6]
maxs [9; 4]
*)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With