“无法将Array {Float64,2}类型的对象`转换为Array类型的对象{Float64,13}

问题描述 投票:-1回答:1

我收到以下错误

ERROR: "Cannot `convert` an object of type Array{Float64,2} to an object of type #Array{Float64,13} This may have arisen from a call to the constructor #Array{Float64,13}(...), since type constructors 

这是我试过的代码:

type buscase
baseMVA::Float64
bus::Array{Float64,13}
gen::Array{Float64,21}
branch::Array{Float64,13}
end

mpc=buscase(100.00,
[1  2   0   0   0   0   1   1   0   230 1   1.1 0.9;
2   1   300 98.61   0   0   1   1   0   230 1   1.1 0.9;
3   2   300 98.61   0   0   1   1   0   230 1   1.1 0.9;
4   3   400 131.47  0   0   1   1   0   230 1   1.1 0.9;
5   2   0   0   0   0   1   1   0   230 1   1.1 0.9;],

[1  40  0   30  -30 1   100 1   40  0   0   0   0   0   0   0   0   0   0   0   0;
1   170 0   127.5   -127.5  1   100 1   170 0   0   0   0   0   0   0   0   0   0   0   0;
3   323.49  0   390 -390    1   100 1   520 0   0   0   0   0   0   0   0   0   0   0   0;
4   0   0   150 -150    1   100 1   200 0   0   0   0   0   0   0   0   0   0   0   0;
5   466.51  0   450 -450    1   100 1   600 0   0   0   0   0   0   0   0   0   0   0   0;],

[1  2   0.00281 0.0281  0.00712 400 400 400 0   0   1   -360    360;
1   4   0.00304 0.0304  0.00658 0   0   0   0   0   1   -360    360;
1   5   0.00064 0.0064  0.03126 0   0   0   0   0   1   -360    360;
2   3   0.00108 0.0108  0.01852 0   0   0   0   0   1   -360    360;
3   4   0.00297 0.0297  0.00674 0   0   0   0   0   1   -360    360;
4   5   0.00297 0.0297  0.00674 240 240 240 0   0   1   -360    360;]
)
arrays constructor julia matpower
1个回答
1
投票

这是你应该如何定义你的结构

struct buscase
   baseMVA::Float64
   bus::Array{Float64,2}
   gen::Array{Float64,2}
   branch::Array{Float64,2}
end

现在你的命令mpc=buscase(100.00, ....将工作

就像它说的那样,Array定义中的数字表示参数的数量。

你也可以使用Matrix类型,它是Array{T,2}的缩写形式:

struct buscase
   baseMVA::Float64
   bus::Matrix{Float64}
   gen::Matrix{Float64}
   branch::Matrix{Float64}
end

最后但并非最不重要的是,如果你想要一个固定大小的数组,你应该看看StaticArrays.jl包。但是,对于最多10-20个元素的阵列(取决于观察到的性能增益),建议使用此软件包。

© www.soinside.com 2019 - 2024. All rights reserved.