如何将矩阵附加到 Julia 中的向量

Moh*_*ani 1 machine-learning neural-network julia

在使用Julia实现 ML 期间,我想创建一个空数组,该数组采用 W 的矩阵,因此所有 W 都用于表示法和索引

像第 1 层 on W[1],对于第 2 层W[2],其中 W 是以下类型Vector{Matrix{Float64}}

我尝试了以下

julia> W = Vector{Matrix{Float64}}()
0-element Array{Array{Float64,2},1}

julia> append!(W, randn(2,3))
ERROR: MethodError: Cannot `convert` an object of type Float64 to an object of type Array{Float64,2}
Closest candidates are:
  convert(::Type{T}, ::AbstractArray) where T<:Array at array.jl:490
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/LinearAlgebra/src/factorization.jl:53
Run Code Online (Sandbox Code Playgroud)

即使我尝试过push!它也返回了一些奇怪的东西

julia> push!(W, randn(2,3))
7-element Array{Array{Float64,2},1}:
 #undef                                                                                                                         
 #undef                                                                                                                         
 #undef                                                                                                                         
 #undef                                                                                                                         
 #undef                                                                                                                         
 #undef                                                                                                                         
    [1.0062340094124418 -0.38626851094866743 -0.33618129619245823; 0.015522767526406687 0.28674191528121296 -1.0633951718710888]
Run Code Online (Sandbox Code Playgroud)

Sim*_*sch 5

真正的解决方案是使用push!而不是append!. 如果您在 REPL 中提供帮助(按?),您可以查看以下文档push!

help?> push!
search: push! pushfirst! pushdisplay

  push!(collection, items...) -> collection

  Insert one or more items at the end of collection.

  Examples
  ??????????

  julia> push!([1, 2, 3], 4, 5, 6)
  6-element Array{Int64,1}:
   1
   2
   3
   4
   5
   6

  Use append! to add all the elements of another collection to collection. The result of the preceding example is
  equivalent to append!([1, 2, 3], [4, 5, 6]).
Run Code Online (Sandbox Code Playgroud)