Reinterpret a Matrix as a Vector of SVectors

I was trying to read the StaticArrays.jl documentation to remap a Matrix as a Vector of SVectors but I get an error when I try to run the example code. Can someone kindly point out the mistake?

julia> using StaticArrays

julia> function svectors(x::Matrix{Float64})
           @assert size(x,1) == 3
           reinterpret(SVector{3,Float64}, x, (size(x,2),))
       end
svectors (generic function with 1 method)

julia> rot = Matrix{Float64}(undef, 3, 10000);

julia> b = svectors(rot);
ERROR: MethodError: no method matching reinterpret(::Type{SArray{Tuple{3},Float64,1,3}}, ::Array{Float64,2}, ::Tuple{Int64})
Closest candidates are:
  reinterpret(::Type{T}, ::Base.ReshapedArray, ::Tuple{Vararg{Int64,N}} where N) where T at reshapedarray.jl:191
  reinterpret(::Type{T}, ::A<:AbstractArray{S,N}) where {T, N, S, A<:AbstractArray{S,N}} at reinterpretarray.jl:14
  reinterpret(::Type{T}, ::Any) where T at essentials.jl:370
  ...
Stacktrace:
 [1] svectors(::Array{Float64,2}) at ./REPL[2]:3
 [2] top-level scope at none:0

I found my answer at Reinterpret an Array of Float64 as an Array of SVector{Float64} ? · Issue #410 · JuliaArrays/StaticArrays.jl · GitHub.

You can do:

b = reinterpret(SVector{3,Float64}, rot[:]);

1 Like