# How to create an SVector of SVectors?

**URL:** <https://discourse.julialang.org/t/how-to-create-an-svector-of-svectors/61025>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 12, 2021, 2:47pm UTC](https://discourse.julialang.org/t/how-to-create-an-svector-of-svectors/61025 "2021-05-12T14:47:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 12, 2021, 2:47pm UTC](https://discourse.julialang.org/t/how-to-create-an-svector-of-svectors/61025/1 "2021-05-12T14:47:54Z")

</div>

The following does not work:

```julia
using StaticArrays

pos=SVector{3, SVector{3,Float64}}(zeros(3,3))

```

Any idea?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 12, 2021, 2:49pm UTC](https://discourse.julialang.org/t/how-to-create-an-svector-of-svectors/61025/2 "2021-05-12T14:49:53Z")

</div>

```julia
julia> pos = zeros(SVector{3,SVector{3,Float64}})
3-element SVector{3, SVector{3, Float64}} with indices SOneTo(3):
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 12, 2021, 2:50pm UTC](https://discourse.julialang.org/t/how-to-create-an-svector-of-svectors/61025/3 "2021-05-12T14:50:18Z")

</div>

The other possibility is that you want an `SMatrix{3,3,Float64}`.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 12, 2021, 2:53pm UTC](https://discourse.julialang.org/t/how-to-create-an-svector-of-svectors/61025/4 "2021-05-12T14:53:52Z")

</div>

> [@lmiq](#):
>
> ` pos = zeros(SVector{3,SVector{3,Float64}})`

What would be the advantages/ disadvantages of an

```julia
(SVector{3,SVector{3,Float64}})

```

compared to a

```julia
SMatrix{3,3,Float64}
?

```

Probably I need:

```julia
(SVector{3,MVector{3,Float64}})

```

actually…

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 12, 2021, 3:46pm UTC](https://discourse.julialang.org/t/how-to-create-an-svector-of-svectors/61025/5 "2021-05-12T15:46:01Z")

</div>

Probably you need either a static matrix or a vector of static vectors (`Vector{SVector{}}`). But of course more information would be needed to discuss that.

The range of uses of mutable-static matrices and vectors is somewhat limited (surprisingly). A vector of static vector can be mutated like this:

```julia
julia> x = [zeros(SVector{3,Float64}) for i in 1:3]
3-element Vector{SVector{3, Float64}}:
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

julia> x[1] = rand(SVector{3,Float64})
3-element SVector{3, Float64} with indices SOneTo(3):
 0.7361897874026004
 0.9443149525219294
 0.6846526276759228

julia> x
3-element Vector{SVector{3, Float64}}:
 [0.7361897874026004, 0.9443149525219294, 0.6846526276759228]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

```

This is very efficient, and even if you want to mutate one of the elements of `x[1]`, doing:

```julia
julia> x[1] = SVector(x[1][1], x[1][2], 0.)
3-element SVector{3, Float64} with indices SOneTo(3):
 0.7361897874026004
 0.9443149525219294
 0.0

```

This is still better than keeping `x[1]` as a mutable vector all the time. Also, there is the `Setfield` package to make that easier:

```julia
julia> using Setfield

julia> @set! x[1][1] = 5.
3-element Vector{SVector{3, Float64}}:
 [5.0, 0.9443149525219294, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

```
