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.
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?
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.
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: