0

我有一个工作正常但具有不同数据类型的元组的函数版本

我想从 excel 中使用它,这意味着当我将数据传递给函数时,我只能使用双精度数组或对象数组。因为我有各种数据类型,所以我将它们全部转换为对象,然后我重写了它并使用了拆箱:

let returnvect (data:obj[][]) (dtsvec:float[]) (inputdate:float) =
let nrhrs = dtsvec |> Array.length
let itera = nrhrs - 1
let res = Array.zeroCreate nrhrs 
let resp = Array.zeroCreate nrhrs
for i=0 to itera do
    for a in data do
        match a.[0] with
        | r when unbox<float> r <= inputdate ->
            match a.[2] , a.[3] with
            | s, t when unbox<float> s <= dtsvec.[i] && unbox<float> t >= dtsvec.[i] -> 
                    res.[i] <- res.[i] + unbox<float> a.[4] 
                    resp.[i] <- resp.[i] + unbox<float> a.[5]  * unbox<float> a.[4] 
            |_ -> 
                res.[i] <- res.[i] + 0.0
                resp.[i] <- resp.[i] + 0.0
        |_ -> 
            res.[i] <- res.[i] + 0.0
            resp.[i] <- resp.[i] + 0.0
let avgprice = dotdivideZ resp res
let inp = [|dtsvec; res; avgprice|]
let newres = MultiArray inp
newres

它通过了类型检查,但是当我运行它时出现以下错误:

System.InvalidCastException: Specified cast is not valid.
 at FSI_0044.returnvect(Object[][] data, Double[] dtsvec, Double inputdate)

请问有什么想法吗?谢谢!

4

1 回答 1

2

问题是数组数组。这有效:

let returnvect (data:obj[,]) (dtsvec:float[]) (inputdate:float) =
    let itera = dtsvec |> Array.length
    let iterb = data.GetLength 0
    let res = Array.zeroCreate itera 
    let resp = Array.zeroCreate itera
    for i=0 to (itera - 1) do
       for j=0 to (iterb - 1) do
        match data.[j,0] with
        | r when unbox<float> r <= inputdate ->
            match data.[j,2] , data.[j,3] with
            | s, t when unbox<float> s <= dtsvec.[i] && unbox<float> t >= dtsvec.[i] -> 
                    res.[i] <- res.[i] + unbox<float> data.[j,4] 
                    resp.[i] <- resp.[i] + unbox<float> data.[j,5]  * unbox<float> data.[j,4] 
            |_ -> 
                res.[i] <- res.[i] + 0.0
                resp.[i] <- resp.[i] + 0.0
        |_ -> 
            res.[i] <- res.[i] + 0.0
            resp.[i] <- resp.[i] + 0.0
let avgprice = dotdivideZ resp res
let inp = [| dtsvec; res; avgprice |]
let newres = MultiArray inp
newres
于 2013-10-09T15:40:50.777 回答