# Finding row index of matrix

**URL:** <https://discourse.julialang.org/t/finding-row-index-of-matrix/65540>\
**Category:** Performance\
**Tags:** indexing, array\
**Created:** [July 30, 2021, 6:04am UTC](https://discourse.julialang.org/t/finding-row-index-of-matrix/65540 "2021-07-30T06:04:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![juls](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@juls](https://discourse.julialang.org/u/juls)\
**Post date:** [July 30, 2021, 6:04am UTC](https://discourse.julialang.org/t/finding-row-index-of-matrix/65540/1 "2021-07-30T06:04:56Z")

</div>

Hi,  
I have two matrices :

```julia
b = 
6×2 Array{Int64,2}:
 0 2
 1 4
 0 7
 1 4
 1 5
 2 4

```

and

```julia
a = 
6×2 Array{Int64,2}:
 0 2
 0 7
 1 4
 1 4
 1 5
 2 4

```

I need to find row index, i of matrix b with respect to a. i.e. a = b(i)

```julia
and get output as array: 
1
3
2
5
4

```

I tried using functions like findall but haven’t got this desired output  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 30, 2021, 9:52am UTC](https://discourse.julialang.org/t/finding-row-index-of-matrix/65540/3 "2021-07-30T09:52:44Z")

</div>

I will not give you a solution but I will at least share that what you are after is **(row) permutation**. Either represented by a permutation vector (the one that you call _row indices_) or a [permutation matrix](https://en.wikipedia.org/wiki/Permutation_matrix).

For convenience I reenter your matrices here (please next time make it easier for others to help you by putting such _code_ here)

```julia
B = [0 2; 1 4; 0 7; 1 4; 1 5; 2 4]
A = [0 2; 0 7; 1 4; 1 4; 1 5; 2 4]

```

The permutation vector is

```julia
p = [1,3,2,4,5,6]

```

There is a fuction called [permute!](https://docs.julialang.org/en/v1/base/arrays/#Base.permute!-Tuple%7BAny,%20AbstractVector%7BT%7D%20where%20T%7D) in Julia, but it only works for vectors and not matrices. We could perhaps reformat the matrix into a vector of row vectors but I will not do it here. Instead, I will form a permutation matrix

```julia
julia> using LinearAlgebra

julia> P = 1I(6)[p,:]
6×6 SparseArrays.SparseMatrixCSC{Int64, Int64} with 6 stored entries:
 1 ⋅ ⋅ ⋅ ⋅ ⋅
 ⋅ ⋅ 1 ⋅ ⋅ ⋅
 ⋅ 1 ⋅ ⋅ ⋅ ⋅
 ⋅ ⋅ ⋅ 1 ⋅ ⋅
 ⋅ ⋅ ⋅ ⋅ 1 ⋅
 ⋅ ⋅ ⋅ ⋅ ⋅ 1

```

and use it to permute the rows of the matrix `B` just by premultiplication.

```julia
julia> A == P*B
true

```

Well, I did not help solve the problem (I did not show how to find the vector `p` or the matrix `P`, I only interpreted them) but at least you may have another keyword to search for. Your are trying to **find a permutation matrix** (or the corresponding vector) to transform one matrix into another by premultiplication. I am afraid you would just have to loop over the rows of the matrices.

---

<div class="post-metadata">

**Author:** ![juls](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@juls](https://discourse.julialang.org/u/juls)\
**Post date:** [July 30, 2021, 10:07am UTC](https://discourse.julialang.org/t/finding-row-index-of-matrix/65540/4 "2021-07-30T10:07:33Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)\
**Post date:** [July 30, 2021, 10:15am UTC](https://discourse.julialang.org/t/finding-row-index-of-matrix/65540/5 "2021-07-30T10:15:16Z")

</div>

This one almost works, although it does not increment for duplicate row entries:

```julia
map(eachrow(b)) do r
       findfirst(i->view(a,i,:)==r, 1:size(a,1))
end

```

```julia
6-element Vector{Int64}:
 1
 3
 2
 3
 5
 6

```

To fix this for repeated elements, something like this works:

```julia
a = [0 2; 0 7; 1 4; 1 4; 1 5; 2 4]
b = [0 2; 1 4; 0 7; 1 4; 1 5; 2 4]

# Build up a dict mapping each row to the row numbers where the row occurs
l = Dict{Vector{Int}, Vector{Int}}()
foreach(1:size(a,1)) do i
    r = a[i,:]
    if haskey(l,r) 
        push!(l[r], i)
    else 
        l[r] = [i]
    end
end
# And collect the values according to their occurences
map(eachrow(b)) do r
    popfirst!(l[r])
end

```

```julia
6-element Vector{Int64}:
 1
 3
 2
 4
 5
 6

```

---

<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:** [July 30, 2021, 11:26am UTC](https://discourse.julialang.org/t/finding-row-index-of-matrix/65540/6 "2021-07-30T11:26:13Z")

</div>

Another take:

```julia
function findrowindex(a,b)
    c = fill(0,size(a,1))
    for i in axes(a,1)
        for j in axes(b,1)
            @views if (b[j,:] == a[i,:]) & !(j in c)
                c[i] = j
                @goto outerloop
            end
        end
        @label outerloop
    end
    return c
end

```

_NB: the goto is not needed but aims at speeding up function_

```julia
c = findrowindex(a,b)
6-element Vector{Int64}:
 1
 3
 2
 4
 5
 6
b[c,:] == a # true

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 30, 2021, 12:15pm UTC](https://discourse.julialang.org/t/finding-row-index-of-matrix/65540/7 "2021-07-30T12:15:43Z")

</div>

> [@zdenek\_hurak](#):
>
> what you are after is **(row) permutation**.

Is this correct? The original question has 6 rows, but a 5-vector answer. Some rows are repeated.

My guess from the words was `indexin`, and possibly `unique`, but…

```julia
julia> q = indexin(collect(eachrow(A)), collect(eachrow(B)))
6-element Vector{Union{Nothing, Int64}}:
 1
 3
 2
 2
 5
 6

julia> A == B[q, :]
true

julia> res = [1, 3, 2, 5, 4]; # from question

julia> B[res, :] # not the same shape as A
5×2 Matrix{Int64}:
 0 2
 0 7
 1 4
 1 5
 1 4

julia> B[unique(q), :]
5×2 Matrix{Int64}:
 0 2
 0 7
 1 4
 1 5
 2 4

julia> indexin(collect(eachrow(B)), collect(eachrow(A))) # reversed, matches @fabiangans's map(eachrow(b))
6-element Vector{Union{Nothing, Int64}}:
 1
 3
 2
 3
 5
 6

```
