在Julia中,是否有可能将Array{Num,1}转换为Array{Float64,1}?

3
我有一个在Julia中使用符号计算的函数。一切都很好,直到绘图的时候。
using Distributions
using Plots
using Symbolics
using SymbolicUtils

function BinomialMeasure(iter::Int64, p::Float64, current_level = nothing)

@variables m0 m1

if current_level == nothing
    current_level = [1]
end
next_level = []
for item in current_level
    append!(next_level, m0*item)
    append!(next_level, m1*item)
end

If iter != 0
    current_level = next_level
    return BinomialMeasure(iter - 1, p , current_level)
else
    return [substitute(i, Dict([m0 => p, m1 => 1 - p])) for i in next_level]
end
end

y = BinomialMeasure(10, 0.4)
x = [( i + 1 ) / length(y) for i = 1:length(y) ]
append!(x, 0)
append!(y,0)
plot(x,y) 

然后它返回以下内容:

MethodError: no method matching AbstractFloat(::Num)
Closest candidates are:
AbstractFloat(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:200
AbstractFloat(::T) where T<:Number at boot.jl:716
AbstractFloat(!Matched::Bool) at float.jl:258 

y是一个Array{Num,1},x是一个Array{Float64,1}。

我尝试过map(float, y),convert(float,y)和float(y),但我认为无法将类型Num转换为Float64,或者至少我不知道该如何做。


1
以防有人来这里想知道是否可以将某种类型的数组转换为另一种类型。通常类似这样的方法是可行的:convert.(Float64, A),其中 A 是一个 Int64Float32 数组等。请注意,在单词 convert 后面加上广播点 . - PatrickT
1个回答

5

您可以在不使用 stringparse 的情况下访问字段 val

y_val = [i.val for i in y]

这当然比解析字符串要具有更好的性能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接