# How to define an element inside a structure with type 3x3 Matrix{Float64}

**URL:** <https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357>\
**Category:** General Usage\
**Tags:** question, mutable-structure\
**Created:** [September 17, 2021, 11:07pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357 "2021-09-17T23:07:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 17, 2021, 11:07pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357/1 "2021-09-17T23:07:31Z")

</div>

I need to define Ls to be of `3x3 Matrix{Float64}`. I could write as below but it is not working, any idea to define this?

```julia
mutable struct R
    Ls::Array{Float64, 3x3} = [0,0,0; 0,0,0; 0,0,0] 
    Ls::Vector{Vector{Float64}} = [0,0,0; 0,0,0; 0,0,0]
end

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 17, 2021, 11:09pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357/2 "2021-09-17T23:09:40Z")

</div>

`Array{Float64, 2}` is a two-dimensional array. The size of the array is not part of the type.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 17, 2021, 11:14pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357/4 "2021-09-17T23:14:08Z")

</div>

> [@PetrKryslUCSD](#):
>
> `Array{Float64, 2}` is a two-dimensional array. The size of the array is not part of the type.

@PetrKryslUCSD Thank you! I have did that, but I get the below error:

```julia
mutable struct R
    Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0]  
end
ERROR: LoadError: syntax: unexpected semicolon in array expression around util.jl:450
Stacktrace:
 [1] top-level scope

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 17, 2021, 11:22pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357/5 "2021-09-17T23:22:08Z")

</div>

> [@Amro](#):
>
> ```julia
> mutable struct R
> Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0]  
> end
> 
> ```

I am doubtful one can have an initializer in the type. I would do

```julia
julia> mutable struct R
           Ls::Array{Float64, 2}
       end

julia> a = R(zeros(3,3))
R([0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])

julia> a.Ls
3×3 Matrix{Float64}:
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

julia>

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 17, 2021, 11:29pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357/6 "2021-09-17T23:29:06Z")

</div>

> [@PetrKryslUCSD](#):
>
> I am doubtful one can have an initializer in the type. I would do

@PetrKryslUCSD  
It is possible from the below reference. However, it works well with vector, but not `Array{Float64, 2}`. Do you have any idea?

> [@Default value of \*some\* fields in a mutable struct](https://discourse.julialang.org/t/default-value-of-some-fields-in-a-mutable-struct/33408/4):
>
> This seems like a great use case for a macro. You might take inspiration from [https://github.com/mauro3/Parameters.jl](https://github.com/mauro3/Parameters.jl) which does something similar: julia\> using Parameters julia\> @with\_kw struct Foo x::Int32 = 1 y::Float64 end Foo julia\> Foo(y = 2) Foo x: Int32 1 …

```julia
julia> using Parameters, Base;
julia> Base.@kwdef mutable struct R
    Br::Vector{Float64} = [0,0,0]
    #Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0] 
end
julia> u = R()
R([0.0, 0.0, 0.0])

```

```julia
julia> Base.@kwdef mutable struct R
           Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0]
       end
ERROR: syntax: unexpected semicolon in array expression around util.jl:450
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 17, 2021, 11:38pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357/7 "2021-09-17T23:38:36Z")

</div>

> [@Amro](#):
>
> ```julia
> Base.@kwdef mutable struct R
> Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0]
> end
> 
> ```

Try

```julia
julia> Base.@kwdef mutable struct R
                  Ls::Array{Float64, 2} = [0 0 0; 0 0 0; 0 0 0]
              end
R

julia>

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 17, 2021, 11:45pm UTC](https://discourse.julialang.org/t/how-to-define-an-element-inside-a-structure-with-type-3x3-matrix-float64/68357/8 "2021-09-17T23:45:20Z")

</div>

> [@PetrKryslUCSD](#):
>
> ```julia
> julia> Base.@kwdef mutable struct R
> Ls::Array{Float64, 2} = [0 0 0; 0 0 0; 0 0 0]
> end
> R
> 
> julia>
> 
> ```

@PetrKryslUCSD Thank you very much, yes it is working now.  
As I though previously that `,` is similar to space ` ` as in Matlab language but it seems in Julia matters.
