如何将Julia数组的数据类型从“Any”更改为“Float64”?

时间:2021-08-17 15:37:09

Is there a function in Julia that returns a copy of an array in a desired type, i.e., an equivalent of numpys astype function? I have an "Any" type array, and want to convert it to a Float array. I tried:

Julia中是否有一个函数返回所需类型的数组副本,即等同于numpys astype函数?我有一个“任何”类型的数组,并希望将其转换为Float数组。我试过了:

new_array = Float64(array)

but I get the following error

但是我收到以下错误

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

I can just write a function that goes through the array and returns a float value of each element, but I find it a little odd if there's no built-in method to do this.

我可以编写一个遍历数组的函数并返回每个元素的浮点值,但是如果没有内置方法可以做到这一点我觉得有点奇怪。

4 个解决方案

#1


12  

Use convert. Note the syntax I used for the first array; if you know what you want before the array is created, you can declare the type in front of the square brackets. Any just as easily could've been replaced with Float64 and eliminated the need for the convert function.

使用转换。注意我用于第一个数组的语法;如果在创建数组之前知道了什么,可以在方括号前面声明类型。任何容易被Float64取代的东西都可以轻松取消转换功能。

julia> a = Any[1.2, 3, 7]
3-element Array{Any,1}:
 1.2
 3  
 7  

julia> convert(Array{Float64,1}, a)
3-element Array{Float64,1}:
 1.2
 3.0
 7.0

#2


7  

You can use:

您可以使用:

new_array = Array{Float64}(array)

new_array = Array {Float64}(数组)

#3


1  

Daniel and Randy's answers are solid, I'll just add another way here I like because it can make more complicated iterative cases relatively succinct. That being said, it's not as efficient as the other answers, which are more specifically related to conversion / type declaration. But since the syntax can be pretty easily extended to other use case it's worth adding:

丹尼尔和兰迪的答案是可靠的,我只是在这里添加另一种方式,因为它可以使更复杂的迭代案例相对简洁。话虽如此,它并不像其他答案那样有效,后者更具体地与转换/类型声明相关。但由于语法可以很容易地扩展到其他用例,因此值得添加:

a = Array{Any,1}(rand(1000))
f = [float(a[i]) for i = 1:size(a,1)]

#4


1  

You can also use the broadcast operator .:

您还可以使用广播运营商。:

a = Any[1.2, 3, 7]
Float64.(a)

#1


12  

Use convert. Note the syntax I used for the first array; if you know what you want before the array is created, you can declare the type in front of the square brackets. Any just as easily could've been replaced with Float64 and eliminated the need for the convert function.

使用转换。注意我用于第一个数组的语法;如果在创建数组之前知道了什么,可以在方括号前面声明类型。任何容易被Float64取代的东西都可以轻松取消转换功能。

julia> a = Any[1.2, 3, 7]
3-element Array{Any,1}:
 1.2
 3  
 7  

julia> convert(Array{Float64,1}, a)
3-element Array{Float64,1}:
 1.2
 3.0
 7.0

#2


7  

You can use:

您可以使用:

new_array = Array{Float64}(array)

new_array = Array {Float64}(数组)

#3


1  

Daniel and Randy's answers are solid, I'll just add another way here I like because it can make more complicated iterative cases relatively succinct. That being said, it's not as efficient as the other answers, which are more specifically related to conversion / type declaration. But since the syntax can be pretty easily extended to other use case it's worth adding:

丹尼尔和兰迪的答案是可靠的,我只是在这里添加另一种方式,因为它可以使更复杂的迭代案例相对简洁。话虽如此,它并不像其他答案那样有效,后者更具体地与转换/类型声明相关。但由于语法可以很容易地扩展到其他用例,因此值得添加:

a = Array{Any,1}(rand(1000))
f = [float(a[i]) for i = 1:size(a,1)]

#4


1  

You can also use the broadcast operator .:

您还可以使用广播运营商。:

a = Any[1.2, 3, 7]
Float64.(a)