zlo*_*lon 0 type-conversion julia
我在朱莉娅很新,所以也许这是一个愚蠢的问题.我有以下代码:
a = [1.0, 2.0];
b = [2.2, 3.1];
Int(a.>b)
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误:
MethodError: Cannot `convert` an object of type BitArray{1} to an object of type Int64
This may have arisen from a call to the constructor Int64(...),
since type constructors fall back to convert methods.
Stacktrace:
[1] Int64(::BitArray{1}) at ./sysimg.jl:77
[2] include_string(::String, ::String) at ./loading.jl:522
Run Code Online (Sandbox Code Playgroud)
该命令1(a.>b)效果很好.你能解释一下:
为什么我的隐式转换不起作用?
a.>b是类型的BitArray{1}.随着Int(a.>b)你尝试转换一个数组,即BitArray,以一个整数,这没有任何意义.
相反,您可能希望将数组的元素转换为整数:
julia> a = [1.0, 2.0];
julia> b = [2.2, 3.1];
julia> Int.(a.>b)
2-element Array{Int64,1}:
0
0
Run Code Online (Sandbox Code Playgroud)
注意点Int.(a.>b)其中广播转化为每一个元素.
之所以1(a.>b)作品是因为它被翻译成了1*(a.>b).这是数字和数组的乘法,这是一个逐元素的操作.