# Indexing assignment to arrays

**URL:** <https://discourse.julialang.org/t/indexing-assignment-to-arrays/57008>\
**Category:** General Usage\
**Tags:** documentation, indexing, arrays, base\
**Created:** [March 12, 2021, 9:17am UTC](https://discourse.julialang.org/t/indexing-assignment-to-arrays/57008 "2021-03-12T09:17:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pshsr](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pshsr](https://discourse.julialang.org/u/pshsr)\
**Post date:** [March 12, 2021, 9:17am UTC](https://discourse.julialang.org/t/indexing-assignment-to-arrays/57008/1 "2021-03-12T09:17:50Z")

</div>

The [documentation for indexing assignment](https://docs.julialang.org/en/v1/manual/arrays/#man-indexed-assignment) in the Julia manual says that:

> If any index `I_k` selects more than one location, then the right hand side `X` must be an array with the same shape as the result of indexing `A[I_1, I_2, ..., I_n]` or a vector with the same number of elements. The value in location `I_1[i_1], I_2[i_2], ..., I_n[i_n]` of `A` is overwritten with the value `X[I_1, I_2, ..., I_n]` , converting if necessary.

However, I am not sure if this is a bug, but the behavior in 1.5.3 seems to be slightly different. Consider the following definitions:

```julia
julia> A = zeros(10, 10); #10 x 10 Array{T, 2}
                                                                   
julia> b = ones(100); #100 element Array{T, 1}
                                                                   
julia> s = zeros(1, 100); #1 x 100 Array{T, 2}
                                                                   
julia> t = zeros(100, 1); # 100 x 1 Array{T, 2}

```

Now as documented,

```julia
julia> A[:, :] = b # works as documented

```

fills A with ones. The behavior with `s` and `t`, however, is a bit puzzling. Neither of them are vectors. However,

```julia
julia> A[:, :] = s

```

fills `A` with zeros, even though this behavior does not seem to be documented in the quote from the “Indexing assignment” section reproduced above, as `s` neither has the same dimensions as `A`, nor is it a “vector”. Indeed,

```julia
julia> A[:, :] = t

```

raises precisely this error:

```julia
ERROR: DimensionMismatch("tried to assign 100×1 array to 10×10 destination")

```

So my question is the following: is it due to a documented feature that the assignment with `s` above works, but the one with `t` fails?

Julia version: 1.5.3

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 12, 2021, 10:37am UTC](https://discourse.julialang.org/t/indexing-assignment-to-arrays/57008/2 "2021-03-12T10:37:03Z")

</div>

[Broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting) “recycles” values along singleton indices. Perhaps this example will help:

```julia
julia> A = collect(reshape(1:9, 3, 3))
3×3 Matrix{Int64}:
 1 4 7
 2 5 8
 3 6 9

julia> s = collect(reshape(1:3, 1, 3))
1×3 Matrix{Int64}:
 1 2 3

julia> A .= s
3×3 Matrix{Int64}:
 1 2 3
 1 2 3
 1 2 3

```

---

<div class="post-metadata">

**Author:** ![pshsr](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pshsr](https://discourse.julialang.org/u/pshsr)\
**Post date:** [March 12, 2021, 11:17am UTC](https://discourse.julialang.org/t/indexing-assignment-to-arrays/57008/3 "2021-03-12T11:17:48Z")

</div>

The broadcasting rules do not apply to the examples in the question, since all the arrays (`A, s, t, b`) have the same number of elements. Thus, there is no opportunity or reason for “recycling” entries.

The same behavior as described in the question also happens with arrays constructed from `reshape` objects though:

```julia
julia> A = collect(reshape(1:9, 3, 3))
3×3 Array{Int64,2}:
 1 4 7
 2 5 8
 3 6 9

julia> s = collect(reshape(10:18, 1, 9))
1×9 Array{Int64,2}:
 10 11 12 13 14 15 16 17 18

julia> A[:, :] = s
1×9 Array{Int64,2}:
 10 11 12 13 14 15 16 17 18

julia> A
3×3 Array{Int64,2}:
 10 13 16
 11 14 17
 12 15 18

julia> t = collect(reshape(20:28, 9, 1))
9×1 Array{Int64,2}:
 20
 21
 22
 23
 24
 25
 26
 27
 28

julia> A[:, :] = t
ERROR: DimensionMismatch("tried to assign 9×1 array to 3×3 destination")

```

So again, assigning a 1 x 9 array (`s` in the example) into `A[:, :]`, when `A` is a `3 x 3 Array{T, 2}` works, but assigning a 9 x 1 array (`t` in the example) doesn’t. My interpretation of the quote from the documentation is that neither should work (since neither is a vector, and neither is 3 x 3), but I just want to check if there is something else in the language that allows one but not the other.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 14, 2021, 9:33am UTC](https://discourse.julialang.org/t/indexing-assignment-to-arrays/57008/4 "2021-03-14T09:33:02Z")

</div>

> [@pshsr](#):
>
> broadcasting rules do not apply

Indeed you are right — I was imagining dots where there weren’t any.

I think that according to the [comment here](https://github.com/JuliaLang/julia/blame/95a34a902061699205d0d116878e766f1e338d20/base/indices.jl#L197-L201), `A[:, :] = t` above could be allowed.

Please consider opening an issue about this.

---

<div class="post-metadata">

**Author:** ![pshsr](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pshsr](https://discourse.julialang.org/u/pshsr)\
**Post date:** [March 14, 2021, 12:37pm UTC](https://discourse.julialang.org/t/indexing-assignment-to-arrays/57008/5 "2021-03-14T12:37:13Z")

</div>

Thanks. We now have [an issue](https://github.com/JuliaLang/julia/issues/40018#issue-831145521). 🙂
