如何在结构体中声明矩阵?

rei*_*i w 11 julia

在我的代码中

mutable struct frame
    Lx::Float64
    Ly::Float64
    T::Matrix{Float64}  #I think the error is here

    function frame(
        Lx::Float64,
        Ly::Float64,
        T::Matrix{Float64}
    )
        return new(Lx, Ly, T)
    end
end

frames = frame[]
push!(frames, frame(1.0, 1.0, undef)) #ERROR here I try nothing, undef, any
push!(frames, frame(2.0, 1.0, undef)) #ERROR here I try nothing, undef, any

frames[1].T = [1 1 2]
frames[2].T = [[2 4 5 6] [7 6 1 8]]
Run Code Online (Sandbox Code Playgroud)

我收到以下错误::Matrix

ERROR: MethodError: no method matching frame(::Float64, ::Float64, ::UndefInitializer)
Closest candidates are:
  frame(::Float64, ::Float64, ::Matrix) 
Run Code Online (Sandbox Code Playgroud)

我需要在结构内部定义无量纲矩阵,然后传递不同维度的矩阵,但是当我推送时出现错误!

mca*_*ott 8

该错误是因为您调用的类型没有方法:

julia> methods(frame)
# 1 method for type constructor:
 [1] frame(Lx::Float64, Ly::Float64, T::Matrix{Float64})

julia> typeof(undef)
UndefInitializer
Run Code Online (Sandbox Code Playgroud)

通过使用更少的参数进行调用,可以创建具有未定义字段的可变结构new

julia> mutable struct Frame2
           Lx::Float64
           Ly::Float64
           T::Matrix{Float64} 
           Frame2(x,y) = new(x,y)
           Frame2(x,y,z) = new(x,y,z)
       end

julia> a = Frame2(1,2)
Frame2(1.0, 2.0, #undef)

julia> b = Frame2(3,4,[5 6])
Frame2(3.0, 4.0, [5.0 6.0])

julia> a.T = b.T;

julia> a
Frame2(1.0, 2.0, [5.0 6.0])
Run Code Online (Sandbox Code Playgroud)


Osc*_*ith 5

你要frame(1.0, 1.0, Matrix{Float64}(undef, 0, 0))