# Finding all rows that are the same as a "focus" row

**URL:** https://discourse.julialang.org/t/finding-all-rows-that-are-the-same-as-a-focus-row/60791
**Category:** New to Julia
**Created:** [May 8, 2021, 7:10pm UTC](https://discourse.julialang.org/t/finding-all-rows-that-are-the-same-as-a-focus-row/60791 "2021-05-08T19:10:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)
#### Post date: [May 8, 2021, 7:10pm UTC](https://discourse.julialang.org/t/finding-all-rows-that-are-the-same-as-a-focus-row/60791/1 "2021-05-08T19:10:43Z")

</div>

I have a Matrix, M. Any row in M contain 1’s and NaN’s. For row 20, for example, I can find the ids of all columns that do not contain NaN:

idfocus = findall(x-\>~isnan(x),A[20,:])

I am now interested in finding all (if any) rows above 20 (again, just for example) that contain exactly the same id’s as in idfocus.

What is the most efficient way to solve that problem? Is it looping through, from 1 to 19, say, using the code above, and checking if the intersect of ids found in each row and idfocus has the same length? Can that approach not be improved upon?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [May 9, 2021, 12:22am UTC](https://discourse.julialang.org/t/finding-all-rows-that-are-the-same-as-a-focus-row/60791/3 "2021-05-09T00:22:10Z")

</div>

Check if the elements of indices `idfocus` of the other columns are also NaNs, without storing any new array. If they are, run over the other elements of that row to see if any other element is a NaN.

Store only the row indexes that match.

---

<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: [May 9, 2021, 8:04am UTC](https://discourse.julialang.org/t/finding-all-rows-that-are-the-same-as-a-focus-row/60791/4 "2021-05-09T08:04:39Z")

</div>

> [@Nash](#):
>
> What is the most efficient way to solve that problem?

Perhaps provide an MWE that generates typical data, so that people can benchmark.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [May 9, 2021, 10:43pm UTC](https://discourse.julialang.org/t/finding-all-rows-that-are-the-same-as-a-focus-row/60791/5 "2021-05-09T22:43:00Z")

</div>

Based on the context, two main optimizations can be done. First, assuming that `NaN`s are much fewer than `Number`s, then finding `NaN`s makes more sense. Second, using a loop to prevent allocations can help esepcially if we `break` early at the first difference.

Here is a contrived MWE with size 200:

```julia
function testnan(a,N) 
    indices = Int[]
    idfocus = findall(isnan,a[N,:])
    lastidx = last(idfocus)
    for i in 1:N-1
        colindx = 1
        for j in idfocus
            isnan(a[i,j]) || break
            colindx = i
        end
        colindx == lastidx && push!(indices, i)
    end
    indices
end 

a = rand([0:99;fill(NaN,10)], 200, 200)

using BenchmarkTools
@btime testnan($a, 200) 
  1.070 μs (7 allocations: 2.20 KiB)

```
