# How to return one value for a Matrix comparison with a value?

**URL:** <https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 12, 2022, 7:15pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159 "2022-09-12T19:15:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 12, 2022, 7:15pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159/1 "2022-09-12T19:15:35Z")

</div>

I want to compare the values of a matrix with a value (such such if bigger than zero) and return true if so. How can I do that, cause below is returning a mtrix?

```julia
julia> RR=ones(5,10)
5×10 Matrix{Float64}:
 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
julia> RR[:,1:5] .> 0
5×5 BitMatrix:
 1 1 1 1 1
 1 1 1 1 1
 1 1 1 1 1
 1 1 1 1 1
 1 1 1 1 1

```

---

<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:** [September 12, 2022, 7:21pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159/2 "2022-09-12T19:21:36Z")

</div>

This?

```julia
all(x -> x > 0, RR)

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 12, 2022, 7:27pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159/3 "2022-09-12T19:27:29Z")

</div>

> [@lmiq](#):
>
> `all(x -> x > 0, RR)`

Thank you very much!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 12, 2022, 7:39pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159/4 "2022-09-12T19:39:53Z")

</div>

Or

```julia
all(>(0), RR) 

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 12, 2022, 8:56pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159/5 "2022-09-12T20:56:16Z")

</div>

> [@DNF](#):
>
> `all(>(0), RR) `

When I do the below comparison:

```julia
all(<(0.00086), abs.( RR .-1))

```

Does it calculate `abs.( RR .-1)` at each element of RR then compare then element with (0.00086)? If so, is there another more efficient way in terms for performance to calculate `abs.( RR .-1)` for all elements of RR then apply all()`on the resulting matrix`abs.( RR .-1)`?

This is because when I put the above function in a time-loop with RR[10,4000], the speed of the whole code becomes slow.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 12, 2022, 9:33pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159/6 "2022-09-12T21:33:01Z")

</div>

Yes, it first calculates the matrix `abs.(RR .- 1)`, then does the comparison.

And, yes, there are more efficient ways. Here are two:

Make a slightly more complicated anonymous function:

```julia
all(x->abs(x-1)<0.00086, RR)

```

Use a generator:

```julia
all(abs(x-1)<0.00086 for x in RR)

```

They are the same speed, as far as I can tell, approximately 3x as fast as your method.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 12, 2022, 9:35pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159/7 "2022-09-12T21:35:19Z")

</div>

BTW, if you only want to compare a part of the matrix, as it looked like in your OP:

> [@Amro](#):
>
> `RR[:,1:5] .> 0`

then you can use a view to avoid any allocations, since ordinary slices do allocate:

```julia
all(x->abs(x-1)<0.00086, @view RR[:, 1:5])

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 13, 2022, 2:24pm UTC](https://discourse.julialang.org/t/how-to-return-one-value-for-a-matrix-comparison-with-a-value/87159/8 "2022-09-13T14:24:28Z")

</div>

> [@DNF](#):
>
> And, yes, there are more efficient ways. Here are two:

Very nice yes, they are faster

> [@DNF](#):
>
> `all(x->abs(x-1)<0.00086, @view RR[:, 1:5])`

Thank you very much for your feedback.
