I have a file that I can read from it a specific structure Rs
and its instance R
(a simple MWE of it is below)
using LinearAlgebra, Parameters, Base
Base.@kwdef mutable struct Rs
I::Vector{Float64} = [2,2,2]
Dot::Array{Float64, 2} = [1 1 1; 2 2 2; 3 3 3]
end
R = Rs[];
push!(R, Rs());
push!(R, Rs());
julia> R
2-element Vector{Rs}:
Rs([2.0, 2.0, 2.0], [1.0 1.0 1.0; 2.0 2.0 2.0; 3.0 3.0 3.0])
Rs([2.0, 2.0, 2.0], [1.0 1.0 1.0; 2.0 2.0 2.0; 3.0 3.0 3.0])
I want to create RR
to be of Rs, in which each field of RR
is the aggregated vector/array of the field rows of R
, i.e.,
RR = Rs(reduce(vcat, getproperty(r, :I) for r in R), reduce(vcat, getproperty(r, :Dot) for r in R));
julia> RR
Rs([2.0, 2.0, 2.0, 2.0, 2.0, 2.0], [1.0 1.0 1.0; 2.0 2.0 2.0; 3.0 3.0 3.0; 1.0 1.0 1.0; 2.0 2.0 2.0; 3.0 3.0 3.0])
The previous way is working. However, the fields of Rs
are a lot. So, is there an alternative easier way to build RR
rather than long command i.e., RR = Rs(reduce(..),reduce(..), reduce(..)..)
?
Another issue is; if I want to do some operations (such as below), working on RR
or R
would be faster?
for i in eachindex(R)
for j in 1:3
R[i].I[j] = R[1].Dot [j,1];
end
end
for i in 1:length(R.I)/3
for j in 1:3
R.I[3*i+j-3] = R.Dot [3*i+j-3,1];
end
end