# Vector of MVectors

**URL:** <https://discourse.julialang.org/t/vector-of-mvectors/128275>\
**Category:** General Usage\
**Tags:** question, package, staticarrays\
**Created:** [April 21, 2025, 10:03pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275 "2025-04-21T22:03:27Z")\
**Posts on this page:** 10\
**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:** [April 21, 2025, 10:03pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/1 "2025-04-21T22:03:27Z")

</div>

What am I doing wrong? I want to have a vector of MVectors, but each of them shall be different.

```julia
using StaticArrays
vec=zeros(MVector{3, Float64}, 4)
vec[1] .= [1,2,3]
vec

```

has as output:

```julia
 [1.0, 2.0, 3.0]
 [1.0, 2.0, 3.0]
 [1.0, 2.0, 3.0]
 [1.0, 2.0, 3.0]

```

but I expected:

```julia
 [1.0, 2.0, 3.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

```

How can I fix this?

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [April 21, 2025, 10:09pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/2 "2025-04-21T22:09:42Z")

</div>

Use `vec[1] = [1,2,3]`

---

<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:** [April 21, 2025, 10:12pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/3 "2025-04-21T22:12:43Z")

</div>

> [@alfaromartino](#):
>
> vec[1] = [1,2,3]

Not the answer I am looking for. I want to create an array of distinct MVectors at the very beginning to allocated the required memory before logging my real-time data.

This approach would also fail in reality because I am not assigning [1,2,3], but a vector that is always the same vector, just with different values.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 21, 2025, 10:20pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/4 "2025-04-21T22:20:50Z")

</div>

> [@ufechner7](#):
>
> I want to have a vector of MVectors,

Side question: why a vector of `MVector`? If you have an array of `SVector` it will typically be more efficient and you can still mutate the array contents:

```julia
julia> using StaticArrays

julia> vec = zeros(SVector{3, Float64}, 4)
4-element Vector{SVector{3, Float64}}:
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

julia> vec[1] = @SVector[1,2,3];

julia> vec
4-element Vector{SVector{3, Float64}}:
 [1.0, 2.0, 3.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

```

This is analogous to the fact that numbers are immutable (you can’t [change the value of `4`](https://softwareengineering.stackexchange.com/questions/254799/ever-change-the-value-of-4-how-did-this-come-into-hayes-thomas-quiz)), but if you have an array of numbers you can mutate the _container_ to replace an element with a different number.

PS. `vec[1] = [1,2,3]` also works, but unnecessarily heap-allocates a `Vector` for the right-hand-side before converting it to an `SVector` to store in the array `vec`.

> This approach would also fail in reality because I am not assigning [1,2,3], but a vector that is always the same vector, just with different values.

PPS. If the vector is an `SVector`, you don’t need to worry about pre-allocating it and then overwriting it in place. Part of the whole point of using StaticArrays is that “allocating” them as local variables is essentially free, in the same way that you don’t worry about “allocating” new integers when you compute `n = n + 1` in a loop.

---

<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:** [April 21, 2025, 10:30pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/5 "2025-04-21T22:30:59Z")

</div>

> [@ufechner7](#):
>
> ```julia
> using StaticArrays
> vec=zeros(MVector{3, Float64}, 4)
> vec[1] .= [1,2,3]
> vec
> 
> ```

I use now:

```julia
using StaticArrays
vec=zeros(MVector{3, Float64}, 4)
vec[1] = copy([1,2,3])
vec

```

Which works as expected.

But the behavior shown in the first post is still unexpected. Can anybody explain what is going on there? Is that a bug?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 21, 2025, 10:39pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/6 "2025-04-21T22:39:29Z")

</div>

> [@ufechner7](#):
>
> ```julia
> using StaticArrays
> vec=zeros(MVector{3, Float64}, 4)
> vec[1] .= [1,2,3]
> vec
> 
> ```

This does seem like a bug. Perhaps better demonstrated by `vec[1][1] = 99` – you want to mutate one element.

`@less zeros(MVector{3, Float64}, (4,))` shows that it’s doing `fill!(vec, zero(T))`. StaticArrays could overload things to avoid this. But perhaps Base shouldn’t be assuming `fill!` is safe?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 21, 2025, 10:41pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/7 "2025-04-21T22:41:26Z")

</div>

> [@ufechner7](#):
>
> But the behavior shown in the first post is still unexpected. Can anybody explain what is going on there? Is that a bug?

It certainly seems like it would be friendlier for the `zeros` function to have a specialized implementation for `MVector`, but I’m not sure it’s a bug: your code is equivalent to `fill(zero(MVector{3, Float64}), 4)`, which only allocates a single `MVector` and re-uses it for all elements, which leads to what you saw if you mutate it. (You’d see the same problem if you tried `vec[1][1] = 3`.)

Do:

```julia
vec = [zero(MVector{3, Float64}) for _=1:4]

```

to ensure that each element of `vec` is a distinct `MVector`.

PS. See also [Current behavior of fill and the like..?](https://discourse.julialang.org/t/current-behavior-of-fill-and-the-like/78888) and many other discussions.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [April 21, 2025, 10:44pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/8 "2025-04-21T22:44:25Z")

</div>

This question has come up before. See [this post](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108).

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [April 21, 2025, 10:51pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/9 "2025-04-21T22:51:52Z")

</div>

To answer the original question, use

```julia
vec = [MVector(0.,0.,0.) for _ in 1:4]

```

Then `vec[1] .= [1,2,3]` produces

```julia
julia> vec
4-element Vector{MVector{3, Float64}}:
 [1.0, 2.0, 3.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0]

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 21, 2025, 10:55pm UTC](https://discourse.julialang.org/t/vector-of-mvectors/128275/10 "2025-04-21T22:55:18Z")

</div>

`zeros` is [`fill`-like](https://github.com/JuliaLang/julia/blob/760b2e5b7396f9cc0da5efce0cadd5d1974c4069/base/array.jl#L582). You did actually broadcast over the 3 elements of `vec[1]`, but `vec` had 4 of the same objects so you see it everywhere.

```julia
julia> x = zeros(BigInt, 2); x[1] === x[2] # mutable type
true

julia> x = [zero(BigInt) for i in 1:2]; x[1] === x[2]
false

```

IMO it should be documented and perhaps have an option for separate instantiation.

This is the old issue debating it: [`zeros` etc. is arguably broken for mutable element types · Issue #29168 · JuliaLang/julia](https://github.com/JuliaLang/julia/issues/29168). I would’ve definitely been on the side of “if I wanted the same instance, I’d use `fill(zero(T), dims...)`,” but that’s a subjective expectation and enough people went the other way. There also seemed to be an expectation that each element has a numeric type with immutable API (even the technically mutably implemented `BigInt`), which `StaticArrays` is deliberately flying in the face of; think about it, would we ever `zero(Vector{Float64})`? It’s understandable because there isn’t an API for filling arbitrary `AbstractArray`s (`zeros`/`ones`/`fill` only instantiate `Array`, and `zeros`/`ones` document so), and `StaticArrays` straddle the line between scalar and array.
