我有一个功能正常的版本,但有一个不同数据类型的元组
我想从excel中使用它,这意味着当我将数据传递给函数时,我只能使用双精度数或对象数组。因为我有varius数据类型,所以我将它们全部转换为对象,然后我重写了这个并使用了取消装箱:
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)
有什么想法吗?谢谢!
答案 0 :(得分: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