# Finding row of approximate values in array

**URL:** <https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 21, 2018, 2:43am UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550 "2018-07-21T02:43:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazzieman](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jazzieman](https://discourse.julialang.org/u/jazzieman)\
**Post date:** [July 21, 2018, 2:43am UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550/1 "2018-07-21T02:43:40Z")

</div>

I have a row vector ‘a’ with a given coordinate (x, y, z) and I need to obtain the row in a coordinates array (B) with the coordinates are closest to the row vector ‘a’. I’ve been using a function that I found on the web:

```julia
matchrow(a,B) = findfirst(i->all(j->a[j] == B[i,j],1:size(B,2)),1:size(B,1))

```

but it doesn’t work for approximates (for a given tolerance). Is there a way of doing that?

---

<div class="post-metadata">

**Author:** ![gpapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpapo/32/1857_2.png) [@gpapo](https://discourse.julialang.org/u/gpapo)\
**Post date:** [July 21, 2018, 10:31am UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550/2 "2018-07-21T10:31:09Z")

</div>

Try using `isapprox` instead of `==`.

---

<div class="post-metadata">

**Author:** ![jazzieman](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jazzieman](https://discourse.julialang.org/u/jazzieman)\
**Post date:** [July 21, 2018, 3:41pm UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550/3 "2018-07-21T15:41:43Z")

</div>

I already tried `isapprox` but don’t know where to use it in the matchrow function.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [July 21, 2018, 3:54pm UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550/4 "2018-07-21T15:54:50Z")

</div>

You’ve written `a[j] == B[i,j]` for the comparison, but `==` is just a function that returns `true`/`false`, and you just need to put `isapprox` in its place. There’s a built-in infix `isapprox` operator `≈` which uses default tolerances, and if `rtol=sqrt(eps), atol=0` suffices for your purposes, you could write `a[j] ≈ B[i,j]`. Otherwise, replace the comparison with `isapprox(a[j], B[i,j], atol=???, rtol=???)`.

---

<div class="post-metadata">

**Author:** ![gpapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpapo/32/1857_2.png) [@gpapo](https://discourse.julialang.org/u/gpapo)\
**Post date:** [July 21, 2018, 3:56pm UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550/5 "2018-07-21T15:56:32Z")

</div>

> [@jazzieman](#):
>
> ```julia
> matchrow(a,B) = findfirst(i->all(j->a[j] == B[i,j],1:size(B,2)),1:size(B,1))
> 
> ```

`matchrow(a,B) = findfirst(i->all(j -> isapprox(a[j], B[i, j], atol = 1E-5), 1:size(B,2)), 1:size(B,1))`

---

<div class="post-metadata">

**Author:** ![jazzieman](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jazzieman](https://discourse.julialang.org/u/jazzieman)\
**Post date:** [July 21, 2018, 4:28pm UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550/6 "2018-07-21T16:28:43Z")

</div>

> [@gpapo](#):
>
> matchrow(a,B) = findfirst(i-\>all(j -\> isapprox(a[j], B[i, j], atol = 1E-5), 1:size(B,2)), 1:size(B,1))

Thanks a lot!!! That worked!!! I was stuck there trying to figure it out.

---

<div class="post-metadata">

**Author:** ![jazzieman](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jazzieman](https://discourse.julialang.org/u/jazzieman)\
**Post date:** [July 21, 2018, 4:36pm UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550/7 "2018-07-21T16:36:18Z")

</div>

Thanks!! I migrating to Julia and still trying to figure it out.

---

<div class="post-metadata">

**Author:** ![gpapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpapo/32/1857_2.png) [@gpapo](https://discourse.julialang.org/u/gpapo)\
**Post date:** [July 22, 2018, 11:36am UTC](https://discourse.julialang.org/t/finding-row-of-approximate-values-in-array/12550/8 "2018-07-22T11:36:02Z")

</div>

Thus, may I suggest to use something (for me) much more clear and efficient like an explicit loop e.g.

```julia
function matchrow(a, B::AbstractMatrix; kw...)
    r = 1
    while r ≤ size(B, 1)
        c = 1
        while c ≤ size(B, 2) && isapprox(a, B[r, c], kw...)
            c += 1
        end
        c > size(B, 2) && return r
        r += 1
    end
    return nothing
end

```
