# Assignment of values to a subset of a subset of indices of an array

**URL:** <https://discourse.julialang.org/t/assignment-of-values-to-a-subset-of-a-subset-of-indices-of-an-array/116369>\
**Category:** New to Julia\
**Created:** [June 29, 2024, 1:37am UTC](https://discourse.julialang.org/t/assignment-of-values-to-a-subset-of-a-subset-of-indices-of-an-array/116369 "2024-06-29T01:37:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oscarvdvelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscarvdvelde/32/202157_2.png) [@oscarvdvelde](https://discourse.julialang.org/u/oscarvdvelde)\
**Post date:** [June 29, 2024, 1:37am UTC](https://discourse.julialang.org/t/assignment-of-values-to-a-subset-of-a-subset-of-indices-of-an-array/116369/1 "2024-06-29T01:37:18Z")

</div>

Why does the following give different results?

```julia
a = zeros(Float32, 10)
b = rand(Float32,10)
ind1 = [3,4,6,9]
ind2 = [2,3,4]

```

```julia
julia> a[ind1[ind2]] .= mean(b[ind1[ind2]])
3-element view(::Vector{Float32}, [4, 6, 9]) with eltype Float32:
 -0.45284092
 -0.45284092
 -0.45284092

julia> a'
1×10 adjoint(::Vector{Float32}) with eltype Float32:
 0.0 0.0 0.0 -0.452841 0.0 -0.452841 0.0 0.0 -0.452841 0.0

```

versus:

```julia
julia> a[ind1][ind2] .= mean(b[ind1][ind2])
3-element view(::Vector{Float32}, [2, 3, 4]) with eltype Float32:
 -0.45284092
 -0.45284092
 -0.45284092

julia> a'
1×10 adjoint(::Vector{Float32}) with eltype Float32:
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

```

In other words, why can we index like b[ind1][ind2] to get a correct series of values (using Int64 indices, not BitVectors) but a value cannot be written to a[ind1][ind2]? Should it not result in a warning if such assignment is done incorrectly?

---

<div class="post-metadata">

**Author:** ![savq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/savq/32/22063_2.png) [@savq](https://discourse.julialang.org/u/savq)\
**Post date:** [June 29, 2024, 3:15am UTC](https://discourse.julialang.org/t/assignment-of-values-to-a-subset-of-a-subset-of-indices-of-an-array/116369/2 "2024-06-29T03:15:26Z")

</div>

The problem is that:

- `a[ind1[ind2]]` first allocates a temporary array `_temp1 = ind1[ind2]`, and then an array `a[_temp1]`, so you’re mutating `a`, whereas
- `a[ind1][ind2]` first creates a temporary array `_temp2 = a[ind1]` and then another array `_temp2[ind2]`, so you’re _not_ mutating `a`, you’re mutating `_temp2`.

> **Meta.@lower MWE**
>
> ```julia-repl
> julia> begin
> a = 1:10
> b = 1:10
> ind1 = [3,4,6,9]
> ind2 = [2,3,4]
> end;
> 
> julia> Meta.@lower a[ind1[ind2]]
> :($(Expr(:thunk, CodeInfo(
> @ none within `top-level scope`
> 1 ─ %1 = a
> │ %2 = Base.getindex(ind1, ind2)
> │ %3 = Base.getindex(%1, %2)
> └── return %3
> ))))
> 
> julia> Meta.@lower b[ind1][ind2]
> :($(Expr(:thunk, CodeInfo(
> @ none within `top-level scope`
> 1 ─ %1 = Base.getindex(b, ind1)
> │ %2 = Base.getindex(%1, ind2)
> └── return %2
> ))))
> 
> ```

* * *

Side note: When trying to debug these things it’s useful to simplify your working example. e.g. not using `rand` and `mean`, just presenting the indexing problem.

---

<div class="post-metadata">

**Author:** ![oscarvdvelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscarvdvelde/32/202157_2.png) [@oscarvdvelde](https://discourse.julialang.org/u/oscarvdvelde)\
**Post date:** [June 29, 2024, 4:08am UTC](https://discourse.julialang.org/t/assignment-of-values-to-a-subset-of-a-subset-of-indices-of-an-array/116369/3 "2024-06-29T04:08:52Z")

</div>

Thanks for the clear explanation and the `Meta.@lower` example. I can see now that `_temp2` has no notion of the location in `a` anymore, just the values. It all makes sense.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 29, 2024, 4:14am UTC](https://discourse.julialang.org/t/assignment-of-values-to-a-subset-of-a-subset-of-indices-of-an-array/116369/4 "2024-06-29T04:14:58Z")

</div>

> [@oscarvdvelde](#):
>
> It all makes sense.

You may want to mark @savq’s answer as the solution.
