# Simple syntax for modifying specific entries of one Array based on indices from another Array?

**URL:** <https://discourse.julialang.org/t/simple-syntax-for-modifying-specific-entries-of-one-array-based-on-indices-from-another-array/47494>\
**Category:** General Usage\
**Tags:** question, arrays\
**Created:** [September 29, 2020, 4:26pm UTC](https://discourse.julialang.org/t/simple-syntax-for-modifying-specific-entries-of-one-array-based-on-indices-from-another-array/47494 "2020-09-29T16:26:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [September 29, 2020, 4:26pm UTC](https://discourse.julialang.org/t/simple-syntax-for-modifying-specific-entries-of-one-array-based-on-indices-from-another-array/47494/1 "2020-09-29T16:26:37Z")

</div>

How would I do this? It seems like there should be some simple syntax for this but if so, I can’t find it.

For example, suppose I have a 10x10 matrix `A`, and a 5x2 matrix `idx`, and the rows of `idx` represent row, column indices into `A`.

```julia
A = rand(0:9, (10, 10))
indices = [1 1; 2 4; 7 7]

```

I’d like to perform some operation (in-place, ideally) on those specific indices of `A`. I’ve noticed that I can use comprehensions:

```julia
[A[i,j] = 42 for (i,j) in eachrow(indices)]

```

But it feels a little strange to be doing assignment in the comprehension like that (maybe my Python bias is showing), and I also wonder about performance (though that’s not really a significant concern for me for the moment; this is just for a toy problem and for the moment I’m looking more for conciseness/expressiveness).

Is it possible to do something more like a `view` but with an arbitrary set of indices? I.e. something like

```julia
(@view_at_indices A indices) .= 42

```

More generally is there a way to combine `view`s so that the combination can be treated identically to a regular `view`? For example, if you have some number of rectangular regions of an image that should all be operated on identically in some fashion.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [September 29, 2020, 5:15pm UTC](https://discourse.julialang.org/t/simple-syntax-for-modifying-specific-entries-of-one-array-based-on-indices-from-another-array/47494/2 "2020-09-29T17:15:51Z")

</div>

How about this for the first part of your question:

```julia
A[CartesianIndex.(Tuple.(eachrow(indices)))] .= 42

```

You can create views with the same syntax, but `hcat` of views makes a copy of the data. I think it you want to combine views, you are better off combining indices and then creating one view.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 29, 2020, 10:20pm UTC](https://discourse.julialang.org/t/simple-syntax-for-modifying-specific-entries-of-one-array-based-on-indices-from-another-array/47494/3 "2020-09-29T22:20:03Z")

</div>

Perhaps a little easier:

```julia
A[CartesianIndex.(eachcol(indices)...)] .= 42

```

Or, with a view, but not any better I think:

```julia
B = view(A, CartesianIndex.(eachcol(indices)...))
B .= 42

```

---

<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:** [September 30, 2020, 9:35am UTC](https://discourse.julialang.org/t/simple-syntax-for-modifying-specific-entries-of-one-array-based-on-indices-from-another-array/47494/4 "2020-09-30T09:35:04Z")

</div>

To complement the helpful answers above: in Julia, it is more natural (and performant) to store those indexes directly in a `Vector` of `CartesianIndex`es.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 30, 2020, 10:54am UTC](https://discourse.julialang.org/t/simple-syntax-for-modifying-specific-entries-of-one-array-based-on-indices-from-another-array/47494/5 "2020-09-30T10:54:09Z")

</div>

Agreed.

To answer the question directly: “Simple syntax for modifying specific entries of one Array based on indices from another Array?” The simple syntax for doing this already exists:

```julia
A[ind] .= 42

```

It’s just that `ind` has to contain _actual indices_, linear or cartesian. The `indices` variable that was provided in the OP didn’t actually contain indices, but was just a 2D array of integers, that could be _interpreted_ as indices. But in order to get to the ‘simple syntax’ it is necessary to either make that conversion, or make `indices` have the right type in the first place.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 30, 2020, 11:07am UTC](https://discourse.julialang.org/t/simple-syntax-for-modifying-specific-entries-of-one-array-based-on-indices-from-another-array/47494/6 "2020-09-30T11:07:44Z")

</div>

You can also `reinterpret` the rows into `CartesianIndex{2}` (in general, `CartesianIndex{N}` for array dimension `N`)

```julia
A[reinterpret(CartesianIndex{2}, indices')] .= 1000

```

To my knowledge, this should be the fastest way to do it.

```julia
julia> @btime let B = view($A, CartesianIndex.(eachcol($indices)...))
       B .= 42
       end;
  2.948 μs (10 allocations: 640 bytes)

julia> @btime $A[CartesianIndex.(Tuple.(eachrow($indices)))] .= 42;
  1.487 μs (15 allocations: 1008 bytes)

julia> @btime $A[reinterpret(CartesianIndex{2}, $indices')] .= 1000
  54.819 ns (0 allocations: 0 bytes)

```
