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 doesn’t hold:
If any index
I_k
is itself an array, then the right hand sideX
must also be an array with the same shape as the result of indexingA[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> 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