开发者

How to average elements in one array based on values in another array?

let tiedArray = [|9.4;1.2;-3.4;-3.4;-3.4;-3.4;-10.0|];

let sortedArray = [|-10.0;-3.4;-3.4;-3.4;-3.4;1.2;9.4|];
let sortedArrayRanks = [|1.;2.;3.;4.;5.;6.;7.|];

let desired_ranked_array = [|1.;3.5;3.5;3.5;3.5;6.;7.|] 

hello-

I am trying to write a function that takes 2 arrays (sortedArray and sortedArrayRanks) and return an output array like the one below. The mapping function in this example would take 2,3,4, and 5 in sortedArrayRanks and see that they all have the same value in开发者_运维技巧 sortedArray, and instead replace all of those numbers in an output array with the average of them (which is 3.5)

What's tripping me up is whether to use recursion or an imperative looping contruct, like loop through sorted array, and see if an item is the same one that preceded it, and then if it matches, check the one before it, etc. How can this be solved? Thanks!


This looks quite similar to F# How to Percentile Rank An Array of Doubles?. Here's a variation of my answer to that question:

let rank arr (ranks:float[]) =
let rev = Array.rev arr
let len = Array.length arr
let first x = Array.findIndex (fun y -> y = x) arr
let last x = len - (Array.findIndex (fun y -> y = x) rev) - 1
let avgR x = ranks.[(first x) .. (last x)] |> Array.average
Array.map avgR arr

This assumes that arr is sorted and that the elements in arr support equality comparisons.


You can:

  • zip elements and their ranks
  • group these pairs by element's value
  • average group ranks
  • form result as array

And write down that algo as script:

let f sortedArray sortedArrayRanks =
  [|
    for v,xs in Array.zip sortedArray sortedArrayRanks |>  Seq.groupBy (fun (v,r) -> v) do
      let n,r = Seq.fold (fun (n,r) (_,r') -> (n+1,r+r')) (0,0.) xs
      let r = r/(float n)
      for i in 1..n do yield r
  |]

> f [|-10.0;-3.4;-3.4;-3.4;-3.4;1.2;9.4|] [|1.;2.;3.;4.;5.;6.;7.|];;
val it : float [] = [|1.0; 3.5; 3.5; 3.5; 3.5; 6.0; 7.0|]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜