【发布时间】:2016-02-18 13:15:55
【问题描述】:
在 Julia 中是否有一个函数可以返回所需类型的数组副本,即等效于 numpys astype function?我有一个“任何”类型的数组,并且想将它转换为浮点数组。我试过了:
new_array = Float64(array)
但我收到以下错误
LoadError: MethodError: `convert` has no method matching
convert(::Type{Float64}, ::Array{Any,2})
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert(::Type{Float64}, !Matched::Int8)
convert(::Type{Float64}, !Matched::Int16)
...
while loading In[140], in expression starting on line 1
in call at essentials.jl:56
我可以只编写一个函数来遍历数组并返回每个元素的浮点值,但是如果没有内置方法来执行此操作,我会觉得有点奇怪。
【问题讨论】:
-
下面有几个答案似乎是正确的。你应该选择你认为最好的作为答案。