# Non scalar indexing in Julia

**URL:** <https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269>\
**Category:** General Usage\
**Tags:** question, package, statistics\
**Created:** [July 12, 2019, 2:47am UTC](https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269 "2019-07-12T02:47:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elita\_Lobo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elita_lobo/32/9267_2.png) [@Elita\_Lobo](https://discourse.julialang.org/u/Elita_Lobo)\
**Post date:** [July 12, 2019, 2:47am UTC](https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269/1 "2019-07-12T02:47:15Z")

</div>

How do I achieve non scalar indexing in Julia?

```julia
julia> l
3×2 Array{Int64,2}:
 1 2
 2 3
 4 5

julia> l[[1,2,3],[1,2,1]]
3×3 Array{Int64,2}:
 1 2 1
 2 3 2
 4 5 4

```

I want to assign be able to assign values to different column value in each row. In the above example, I want to select 1st element from row 1, 2nd element from row 2 and 1st element from row 3 and assign different values to each of these element. In python, I could do this using l[1,2,3],[1,2,1]]=[7,8,9] where l is a nd numpy array.  
Is there an efficient way of doing this other than using loops?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 12, 2019, 2:56am UTC](https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269/2 "2019-07-12T02:56:05Z")

</div>

Why not a loop? It seems like a perfectly reasonable approach to the problem and should be very efficient.

---

<div class="post-metadata">

**Author:** ![Elita\_Lobo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elita_lobo/32/9267_2.png) [@Elita\_Lobo](https://discourse.julialang.org/u/Elita_Lobo)\
**Post date:** [July 12, 2019, 3:25am UTC](https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269/3 "2019-07-12T03:25:07Z")

</div>

Isn’t there any way we can parallelize this? Since each assignment is applied to a different slice of array.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 12, 2019, 3:51am UTC](https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269/4 "2019-07-12T03:51:02Z")

</div>

It’s possible, but without knowing what exactly you’re trying to do there’s no way to be sure. Can you provide an example of what you want?

---

<div class="post-metadata">

**Author:** ![Elita\_Lobo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elita_lobo/32/9267_2.png) [@Elita\_Lobo](https://discourse.julialang.org/u/Elita_Lobo)\
**Post date:** [July 12, 2019, 3:57am UTC](https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269/5 "2019-07-12T03:57:58Z")

</div>

```julia
function non_scalar_indexing(arr::Array{Float32,2}, idx1::Array{Int64,1}, idx2::Array{Int64,1},value::Array{Float32,1})
    index=0
    for idx in zip(idx1,idx2)
        arr[idx[0],idx[1]]=value[index];
       index+=1;
    end
    return arr   
end

```

I also would like to be able to handle case where arr is Array{Float32,3} and value is Array{Float32,2}.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 12, 2019, 4:37am UTC](https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269/6 "2019-07-12T04:37:51Z")

</div>

Loops in Julia _aren’t bad!_ In fact, they’ll often be faster than trying to jam everything onto one line. Note that writing everything on a single line does not “parallelize” it.

I’ve often referred to the kind of indexing you’re after here as “pointwise”, and one way of compactly representing it is with `CartesianIndex`:

```julia
julia> l = [1 2; 2 3; 4 5]
3×2 Array{Int64,2}:
 1 2
 2 3
 4 5

julia> l[CartesianIndex.([1,2,3],[1,2,1])]
3-element Array{Int64,1}:
 1
 3
 4

```

---

<div class="post-metadata">

**Author:** ![Elita\_Lobo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elita_lobo/32/9267_2.png) [@Elita\_Lobo](https://discourse.julialang.org/u/Elita_Lobo)\
**Post date:** [July 12, 2019, 4:44am UTC](https://discourse.julialang.org/t/non-scalar-indexing-in-julia/26269/7 "2019-07-12T04:44:55Z")

</div>

I see. Thank you so much for the info! I assumed that the framework would exploit the non sequential behavior in pointwise operations.
