How To Apply a Function to an Array of float Arrays?

前端 未结 2 1779
执念已碎
执念已碎 2021-01-15 06:50

Let\'s suppose I have n arrays, where n is a variable (some number greater than 2, usually less than 10).

Each array has k elements.

I also have an array of

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-15 07:26

    Something like this did it for me:

    let weights = [|0.6;0.3;0.1|]
    
    let arrs = [| [|0.0453;0.065345;0.07566;1.562;356.6|] ; 
                  [|0.0873;0.075565;0.07666;1.562222;3.66|] ; 
                  [|0.06753;0.075675;0.04566;1.452;3.4556|] |]
    
    let applyWeight x y = x * y
    
    let rotate (arr:'a[][]) = 
        Array.map (fun y -> (Array.map (fun x -> arr.[x].[y])) [|0..arr.Length - 1|]) [|0..arr.[0].Length - 1|]
    
    let weightedarray = Array.map (fun x -> Array.map(applyWeight (fst x)) (snd x)) (Array.zip weights arrs)
    
    let newarrs = Array.map Array.sum (rotate weightedarray)
    
    printfn "%A" newarrs
    

    By the way.. the 0 preceding a float value is necessary.

提交回复
热议问题