Vector of MVectors

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:

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..? and many other discussions.

1 Like