# How to create and fill a vector of Struct as in a Matrix?

**URL:** <https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 6, 2022, 7:03pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156 "2022-01-06T19:03:21Z")\
**Posts on this page:** 13\
**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:** [January 6, 2022, 7:03pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/1 "2022-01-06T19:03:22Z")

</div>

I have a struct of `Rs` and a vector `R` of it. Currently, I am using such the below code to construct R and then change the values of its fields.

```julia
Base.@kwdef mutable struct Rs
    I::Vector{Float64} = [2,2,2]
    Dot::Array{Float64, 2} = [0 0 0; 0 0 0; 0 0 0]
end
R = Rs[];
push!(R, Rs()); # the values of fields may change in the code
push!(R, Rs()); # the values of fields may change in the code
julia> R
2-element Vector{Rs}:
 Rs([2.0, 2.0, 2.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])
 Rs([2.0, 2.0, 2.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])
#Modify the values of the fields, such as
#R[index1].I[index2] = value;

```

Is there any way to construct R and fill the values of its fields as in Matrix, such as below?

```julia
Base.@kwdef mutable struct Rs
    I::Vector{Float64} = [2,2,2]
    Dot::Array{Float64, 2} = [0 0 0; 0 0 0; 0 0 0]
end
R = Vector{Rs}[([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]);
   ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])];

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 6, 2022, 7:06pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/2 "2022-01-06T19:06:27Z")

</div>

> [@Amro](#):
>
> ```julia
> Rs.[([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]), 
> ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])]
> 
> ```

maybe. (Note the dot)

---

<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:** [January 6, 2022, 7:09pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/3 "2022-01-06T19:09:03Z")

</div>

> [@Amro](#):
>
> ```julia
> Rs.[([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]), 
> ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])]
> 
> ```

@leandromartinez98 Thank you!  
I have tried but gave me an error:

```julia
julia> U = Rs.[([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]),
          ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])]
ERROR: syntax: invalid syntax "Rs.[([5, 5, 4], [0 0 0; 0 0 0; 0 0 0]), ([3, 2, 7], [0 0 0; 0 0 0; 0 0 0])]" around REPL[8]:1
Stacktrace:
 [1] top-level scope
   @ REPL[8]:

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 6, 2022, 7:13pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/4 "2022-01-06T19:13:30Z")

</div>

> [@Amro](#):
>
> ```julia
> R = Vector{Rs}[([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]);
> ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])]
> 
> ```

I think you want

```julia
R = map(args -> Rs(args...), [([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]);
   ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])])

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 6, 2022, 7:19pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/5 "2022-01-06T19:19:52Z")

</div>

Uhm… not sure why that does not work.

Edit: maybe just adding a parenthesis:

```julia
struct A x end
@show A.([1, 2])

```

Works

---

<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:** [January 6, 2022, 7:21pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/6 "2022-01-06T19:21:15Z")

</div>

> [@stevengj](#):
>
> ```julia
> R = map(args -> Rs(args...), [([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]);
> ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])])
> 
> ```

@stevengj Thank you! it works.  
@lmiq @stevengj Do you think the structure of R is complicated in Julia and affect the performance?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 6, 2022, 7:24pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/7 "2022-01-06T19:24:26Z")

</div>

If the arrays are that small, use Static Arrays

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 6, 2022, 7:25pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/8 "2022-01-06T19:25:26Z")

</div>

> [@Amro](#):
>
> Do you think the structure of R is complicated in Julia and affect the performance?

If you always have 3-component vectors and 3x3 matrices (or in general small vectors/matrices whose size is always the same, e.g. equal to the number of spatial dimensions in a geometry), then you should [consider using StaticArrays](https://docs.julialang.org/en/v1/manual/performance-tips/#Consider-StaticArrays.jl-for-small-fixed-size-vector/matrix-operations).

---

<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:** [January 6, 2022, 7:25pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/9 "2022-01-06T19:25:32Z")

</div>

> [@Amro](#):
>
> ```julia
> Rs.[([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]), 
> ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])]
> 
> ```

@lmiq is it as below?

```julia
julia> X =Rs.([([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]),
          ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])])
ERROR: MethodError: no method matching Rs(::Tuple{Vector{Float64}, Matrix{Float64}})
Closest candidates are:
  Rs(::Any, ::Any) at REPL[3]:2
  Rs(; I, Dot) at util.jl:450

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 6, 2022, 7:26pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/10 "2022-01-06T19:26:48Z")

</div>

I see, I thought that would work… Cannot really test things now. If you use that often you may want a custom constructor then.

---

<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:** [January 6, 2022, 7:33pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/11 "2022-01-06T19:33:16Z")

</div>

I only need it once at the beginning of my code (input of my network data)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 6, 2022, 8:52pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/12 "2022-01-06T20:52:12Z")

</div>

This is in a sense a mix of Leandro and Steve’s answers.

```julia
   Base.splat(Rs).([([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]);
   ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])])

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 6, 2022, 10:24pm UTC](https://discourse.julialang.org/t/how-to-create-and-fill-a-vector-of-struct-as-in-a-matrix/74156/13 "2022-01-06T22:24:47Z")

</div>

> [@Amro](#):
>
> ```julia
> julia> X =Rs.([([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]),
> ([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])])
> ERROR: MethodError: no method matching Rs(::Tuple{Vector{Float64}, Matrix{Float64}})
> Closest candidates are:
> Rs(::Any, ::Any) at REPL[3]:2
> Rs(; I, Dot) at util.jl:450
> 
> ```

> [@lmiq](#):
>
> I see, I thought that would work…

The problem here is that the constructor does **not** take a _single object_ that is a tuple of two values but instead it takes _two separate objects_. Unfortunately this means you cannot just use broadcast and apply the constructor element-wise to a _single_ vector of _two-element tuples_ but instead you need to apply the constructor element-wise to _two vectors_ instead. I adapted the try above to the correct form below:

```julia
julia> R = Rs.(
    [[5.0, 5.0, 4.0], [3.0, 2.0, 7.0]], # vector of first parameters
    [[0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]] # vector of second parameters
)
2-element Array{Rs,1}:
 Rs([5.0, 5.0, 4.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])
 Rs([3.0, 2.0, 7.0], [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])

```
