# Convert Vector{SVector} to Matrix

**URL:** <https://discourse.julialang.org/t/convert-vector-svector-to-matrix/27939>\
**Category:** Performance\
**Tags:** staticarrays\
**Created:** [August 24, 2019, 4:04pm UTC](https://discourse.julialang.org/t/convert-vector-svector-to-matrix/27939 "2019-08-24T16:04:36Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [March 3, 2023, 2:24am UTC](https://discourse.julialang.org/t/convert-vector-svector-to-matrix/27939/13 "2023-03-03T02:24:14Z")

</div>

> [@Elrod](#):
>
> They’re elements of a mutable array.  
> You can always edit the mutable array `x` in my above example, by replacing one immutable `SVector` with another immutable `SVector`.

For example, consider

```julia-repl
julia> a = [SVector(1,2,3), SVector(4,5,6)]
2-element Vector{SVector{3, Int64}}:
 [1, 2, 3]
 [4, 5, 6]

julia> A = reinterpret(reshape, Int64, a)
3×2 reinterpret(reshape, Int64, ::Vector{SVector{3, Int64}}) with eltype Int64:
 1 4
 2 5
 3 6

```

Here, if I set `A[1,1] = 0`, then `a` also changes:

```julia-repl
julia> A[1,1] = 0
0

julia> a
2-element Vector{SVector{3, Int64}}:
 [0, 2, 3]
 [4, 5, 6]

```

To me, replacing `A[1,1]` seems equivalent to replacing the first entry to `a[1]`. Because `a[1]` is `SVector`, being able to replace its first entry seems strange to me.

But you say that replacing `A[1,1]` is actually equivalent to replacing the entire vector `a[1]`. I am not sure how replacing one element `A[1,1]` could be interpreted as replacing the entire `a[1]` with three entries. Still, I think you are correct, because if we make `a` an `SVector` of `SVector`s, we cannot replace `A[1,1]`:

```julia-repl
julia> a = SVector(SVector(1,2,3), SVector(4,5,6))
2-element SVector{2, SVector{3, Int64}} with indices SOneTo(2):
 [1, 2, 3]
 [4, 5, 6]

julia> A = reinterpret(reshape, Int64, a)
3×2 reinterpret(reshape, Int64, ::SVector{2, SVector{3, Int64}}) with eltype Int64 with indices Base.OneTo(3)×SOneTo(2):
 1 4
 2 5
 3 6

julia> A[1,1] = 0
ERROR: setindex!(::SVector{2, SVector{3, Int64}}, value, ::Int) is not defined.
 Hint: Use `MArray` or `SizedArray` to create a mutable static array
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] setindex!(a::SVector{2, SVector{3, Int64}}, value::SVector{3, Int64}, i::Int64)
   @ StaticArrays ~/Programming/StaticArrays/src/indexing.jl:3
 [3] _setindex_ra!
   @ ./reinterpretarray.jl:648 [inlined]
 [4] setindex!(::Base.ReinterpretArray{Int64, 2, SVector{3, Int64}, SVector{2, SVector{3, Int64}}, true}, ::Int64, ::Int64, ::Int64)
   @ Base ./reinterpretarray.jl:507
 [5] top-level scope
   @ REPL[33]:1

```

---

_[View the full topic](https://discourse.julialang.org/t/convert-vector-svector-to-matrix/27939)._
