# Multidimensional arrays as indices in setindex!: bug or feature?

**URL:** <https://discourse.julialang.org/t/multidimensional-arrays-as-indices-in-setindex-bug-or-feature/94558>\
**Category:** General Usage\
**Tags:** indexing, array, setindex\
**Created:** [February 13, 2023, 1:24pm UTC](https://discourse.julialang.org/t/multidimensional-arrays-as-indices-in-setindex-bug-or-feature/94558 "2023-02-13T13:24:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Az9DBvyVLWBjs8g](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Az9DBvyVLWBjs8g](https://discourse.julialang.org/u/Az9DBvyVLWBjs8g)\
**Post date:** [February 13, 2023, 1:24pm UTC](https://discourse.julialang.org/t/multidimensional-arrays-as-indices-in-setindex-bug-or-feature/94558/1 "2023-02-13T13:24:19Z")

</div>

Hello everyone,

I just found that one can use multidimensional arrays as indices within `setindex!`. They behave as 1d arrays, which is different from their behavior in `getindex`, thus the following assertion from [the docs](https://docs.julialang.org/en/v1/manual/arrays/#man-indexed-assignment) doesn’t hold:

> If any index `I_k` is itself an array, then the right hand side `X` must also 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.

Is this a bug or a feature? Indexing with multidimensional arrays is quite unintuitive, and one should just use 1d arrays if the behavior is identical anyway.

Examples:

```julia
julia> x = [ 1 2 3 4
             5 6 7 8
             9 10 11 12
            13 14 15 16]
4×4 Matrix{Int64}:
  1 2 3 4
  5 6 7 8
  9 10 11 12
 13 14 15 16

julia> y = zero(x); y[[1 2; 3 4], [1 2; 3 4]] = x; y
4×4 Matrix{Int64}:
  1 3 2 4
  9 11 10 12
  5 7 6 8
 13 15 14 16

julia> y = zero(x); y[[1, 3, 2, 4], [1, 3, 2, 4]] = x; y
4×4 Matrix{Int64}:
  1 3 2 4
  9 11 10 12
  5 7 6 8
 13 15 14 16

julia> y[[1 2; 3 4], [1 2; 3 4]]
2×2×2×2 Array{Int64, 4}:
[:, :, 1, 1] =
 1 9
 5 13

[:, :, 2, 1] =
 2 10
 6 14

[:, :, 1, 2] =
 3 11
 7 15

[:, :, 2, 2] =
 4 12
 8 16

```

---

<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:** [February 13, 2023, 2:29pm UTC](https://discourse.julialang.org/t/multidimensional-arrays-as-indices-in-setindex-bug-or-feature/94558/2 "2023-02-13T14:29:56Z")

</div>

The next line is also confusing to me

> 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.

Just seems like if `X` has the same shape as `A[I_1, I_2, ..., I_n]`, then it could have a different shape from `A` and should not be indexed like `A` is. `X[I_1, I_2, ..., I_n]` should have the same shape as `A[I_1, I_2, ..., I_n][I_1, I_2, ..., I_n]`, which doesn’t seem to reach a scalar value.
