I have some code where I need to store coordinates x
of a mesh. I’m using a vector of points (static vectors), i.e.
using StaticArrays
Point{dim} = SVector{dim, Float64}
x = rand(Point{3}, 10)
but some parts of the code (e.g. using WriteVTK) require the coordinates to be in a matrix instead of a vector. Is there anything wrong with adding the following constructor method to convert the above vector to a matrix
Base.Matrix(v::Vector{SVector{S, T}}) where {S, T} = reduce(hcat, v)
and using
m = Matrix(x)
mT = transpose(Matrix(x))
to get the vector as a matrix and matrix transpose? (EDIT: Yes, because it creates a copy it is not efficient)
Is there a more efficient way to do this? (EDIT: Yes, use reinterpret
and reshape
as explained below)
Is there any way to do this without making a copy so that modifying elements of matrix m
will change the corresponding elements of x
? (EDIT: Yes, as above)