# array assignment using an array as indexes

**URL:** <https://discourse.julialang.org/t/array-assignment-using-an-array-as-indexes/17683>\
**Category:** General Usage\
**Created:** [November 18, 2018, 3:30pm UTC](https://discourse.julialang.org/t/array-assignment-using-an-array-as-indexes/17683 "2018-11-18T15:30:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zlewko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlewko/32/19176_2.png) [@zlewko](https://discourse.julialang.org/u/zlewko)\
**Post date:** [November 18, 2018, 3:30pm UTC](https://discourse.julialang.org/t/array-assignment-using-an-array-as-indexes/17683/1 "2018-11-18T15:30:49Z")

</div>

I’m new to Julia and am trying to assign to a constant to locations in a multidimensional array using indexes specified in another array. I can do this with a for loop easily, but am trying to understand how to do it as a comprehension or something else without the loop.

For example:  
a = reshape(1:25,5,5)’  
5×5 Adjoint{Int64,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:  
1 2 3 4 5  
6 7 8 9 10  
11 12 13 14 15  
16 17 18 19 20  
21 22 23 24 25

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 18, 2018, 3:41pm UTC](https://discourse.julialang.org/t/array-assignment-using-an-array-as-indexes/17683/2 "2018-11-18T15:41:45Z")

</div>

If you want to change `a`, then you run into obvious trouble:

```julia
julia> a = reshape(1:25,5,5)';

julia> a[1,2]=3
ERROR: indexed assignment fails for a reshaped range; consider calling collect

```

It even tells you how to fix that:

```julia
julia> a = reshape(collect(1:25),5,5)';

julia> a[1,2]=3
3

```

Or were you asking about:

```julia
julia> A=reshape(collect(1:25), 5, 5);
julia> idxs=3:17;
julia> A[idxs].=-1;
julia> idxs=[1,2,25];
julia> A[idxs] = -10:-8;
julia> A
5×5 Array{Int64,2}:
 -10 -1 -1 -1 21
  -9 -1 -1 -1 22
  -1 -1 -1 18 23
  -1 -1 -1 19 24
  -1 -1 -1 20 -8

```

---

<div class="post-metadata">

**Author:** ![zlewko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlewko/32/19176_2.png) [@zlewko](https://discourse.julialang.org/u/zlewko)\
**Post date:** [November 29, 2018, 2:20am UTC](https://discourse.julialang.org/t/array-assignment-using-an-array-as-indexes/17683/4 "2018-11-29T02:20:44Z")

</div>

Thanks, I didn’t realize this post went through. I had just setup my account and accidentally posted this without finishing writing it. I answered my own question shortly after. Your second answer is closer to what I was looking for.

I was trying to assign a to locations in an array where the locations where specified in another array. For example:

a = zeros(Int64,5,5)  
y=[1,2,2,5,5]  
one\_locs = [CartesianIndex(i,y[i]) for i in 1:length(y)]  
a[one\_locs] .= 1  
5×5 Array{Int64,2}:  
1 0 0 0 0  
0 1 0 0 0  
0 1 0 0 0  
0 0 0 0 1  
0 0 0 0 1

My question was more about how to use the CartesianIndex and what I was having trouble with was I didn’t realize I needed the .= for the assignment.
