gpt4 book ai didi

julia - 如何修复 Julia 中的空数组构造函数?

转载 作者:行者123 更新时间:2023-12-04 10:43:24 28 4
gpt4 key购买 nike

julia> Array(Int8,2,3)
ERROR: MethodError: no method matching Array(::Type{Int8}, ::Int64, ::Int64)
Closest candidates are:
Array(::LinearAlgebra.UniformScaling, ::Integer, ::Integer) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/uniformscaling.jl:395
Stacktrace:
[1] top-level scope at REPL[38]:1

它在更新前工作,但现在要求联盟。

最佳答案

定义数组而不指定其元素值的一种方法如下:

julia> Array{Int8}(undef, 2,3)
2×3 Array{Int8,2}:
0 0 0
0 0 0

那是你想要的吗?



还有许多其他方法可以定义某种类型的数组,尤其是当人们已经知道元素的值时:
julia> zeros(Int8, 2, 3)
2×3 Array{Int8,2}:
0 0 0
0 0 0

julia> ones(Int8, 2, 3)
2×3 Array{Int8,2}:
1 1 1
1 1 1

julia> Int8[1 2 3; 4 5 6]
2×3 Array{Int8,2}:
1 2 3
4 5 6

或者当您已经有一个相同类型的其他数组时:
julia> A = Int8[1 2 3; 4 5 6]
2×3 Array{Int8,2}:
1 2 3
4 5 6

julia> similar(A)
# Uninitialized values
2×3 Array{Int8,2}:
-48 52 -5
-126 100 127

julia> similar(A, 2, 2)
# Uninitialized values
2×2 Array{Int8,2}:
-16 -83
0 94

关于julia - 如何修复 Julia 中的空数组构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59827829/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com