# Index a matrix with two separate row and column index vectors

**URL:** https://discourse.julialang.org/t/index-a-matrix-with-two-separate-row-and-column-index-vectors/110410
**Category:** General Usage
**Tags:** question, indexing, arrays
**Created:** [February 19, 2024, 3:03pm UTC](https://discourse.julialang.org/t/index-a-matrix-with-two-separate-row-and-column-index-vectors/110410 "2024-02-19T15:03:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [February 19, 2024, 3:03pm UTC](https://discourse.julialang.org/t/index-a-matrix-with-two-separate-row-and-column-index-vectors/110410/1 "2024-02-19T15:03:29Z")

</div>

I have a matrix `A` and a row index vector R and a column index vector C.  
I want to get a vector from `A` as `[A[R[1], C[1]], A[R[2], C[2]], ...]`.  
For instance, with `R=[2, 4], C=[3, 1]`, I want `[A[2, 3], A[4, 1]]`.  
Is there any built-in mechanism for this kind of indexing? `A[R, C]` will not work.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [February 19, 2024, 3:18pm UTC](https://discourse.julialang.org/t/index-a-matrix-with-two-separate-row-and-column-index-vectors/110410/2 "2024-02-19T15:18:30Z")

</div>

You may broadcast `getindex` as `getindex.(Ref(A), rowinds, colinds)`

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [February 19, 2024, 4:02pm UTC](https://discourse.julialang.org/t/index-a-matrix-with-two-separate-row-and-column-index-vectors/110410/3 "2024-02-19T16:02:05Z")

</div>

In this specific case, I recommend the `getindex` broadcast suggested above. In a more general or complicated situation, consider using [the `CartesianIndex` type](https://julialang.org/blog/2016/02/iteration/). For example,

```julia-repl
julia> R = [2, 4]; C = [3, 1]; A = [10i+j for i in 1:4, j in 1:4]
4×4 Matrix{Int64}:
 11 12 13 14
 21 22 23 24
 31 32 33 34
 41 42 43 44

julia> CartesianIndex.(R, C)
2-element Vector{CartesianIndex{2}}:
 CartesianIndex(2, 3)
 CartesianIndex(4, 1)

julia> A[CartesianIndex.(R, C)]
2-element Vector{Int64}:
 23
 41

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 19, 2024, 8:23pm UTC](https://discourse.julialang.org/t/index-a-matrix-with-two-separate-row-and-column-index-vectors/110410/4 "2024-02-19T20:23:17Z")

</div>

Not a recommendation, just a different view angle using LinearAlgebra.jl:

```julia
diag(view(A, R, C)) 

```

With TensorCast.jl, we can take a non-allocating view along the diagonal:

```julia
using TensorCast
@cast v[i] := view(A, R, C)[i,i]  

```

this view is equivalent to writing:

```julia
V = view(A, R, C)
view(V, diagind(V))

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [February 20, 2024, 8:26am UTC](https://discourse.julialang.org/t/index-a-matrix-with-two-separate-row-and-column-index-vectors/110410/5 "2024-02-20T08:26:17Z")

</div>

```julia
r,c=size(A)
@. A[(C-1)*r+R]

```
