# Two conditions in a while -\> ERROR: TypeError: non-boolean (BitMatrix) used in boolean context

**URL:** <https://discourse.julialang.org/t/two-conditions-in-a-while-error-typeerror-non-boolean-bitmatrix-used-in-boolean-context/112564>\
**Category:** General Usage\
**Tags:** question, error\
**Created:** [April 5, 2024, 9:37am UTC](https://discourse.julialang.org/t/two-conditions-in-a-while-error-typeerror-non-boolean-bitmatrix-used-in-boolean-context/112564 "2024-04-05T09:37:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefani](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Stefani](https://discourse.julialang.org/u/Stefani)\
**Post date:** [April 5, 2024, 9:37am UTC](https://discourse.julialang.org/t/two-conditions-in-a-while-error-typeerror-non-boolean-bitmatrix-used-in-boolean-context/112564/1 "2024-04-05T09:37:43Z")

</div>

Hallo,

I am new to Julia and this is the first time I ever ask for help in a forum, so please let me know if I can improve my post.

My problem:  
Let dR and dW be functions from R^2 → R^{n \times n}.  
There exists a pair (i,j) such that dR(i,j) \< \epsilon and dW(i,j)\< \epsilon.  
Goal: find (i,j)

Hence I want to run a loop like

_while dR(i,j) .\> 0.01 || dW(i,j).\> 0.01_  
_do something until there is (i,j) such that dR(i,j) \< 0.01 AND dW(i,j) \< 0.01_

However, I get the following **ERROR: TypeError: non-boolean (BitMatrix) used in boolean context**

I am thankful for any help!

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [April 5, 2024, 9:43am UTC](https://discourse.julialang.org/t/two-conditions-in-a-while-error-typeerror-non-boolean-bitmatrix-used-in-boolean-context/112564/2 "2024-04-05T09:43:27Z")

</div>

Hi and welcome!

It’s generally more useful to include your code between triple backticks (ie ``` on a blank line at the start and end).

The error you’re getting here is that `dR(i,j) .> 0.01` calculates a matrix of booleans that are true wherever that condition holds. You simply need

```julia
while any(dR(i,j) .> 0.01) || any(dW(i,j) .> 0.01)

```

The two functions `any` and `all` reduce a collection using `||` or `&&`.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [April 5, 2024, 10:32am UTC](https://discourse.julialang.org/t/two-conditions-in-a-while-error-typeerror-non-boolean-bitmatrix-used-in-boolean-context/112564/3 "2024-04-05T10:32:52Z")

</div>

> [@Stefani](#):
>
> My problem:  
> Let dR and dW be functions from R^2R2R^2 → R^{n \times n}Rn×nR^{n \times n}.  
> There exists a pair (i,j) such that dR(i,j) \< \epsilonϵ\epsilon and dW(i,j)\< \epsilonϵ\epsilon.  
> Goal: find (i,j)

When you say `dR(i, j)`, do you mean the function `dR` evaluated at the point (i, j), or the number located at index ij in the n \times n matrix that is returned by `dR`?.

@screw_dog is correct with some options that might get rid of the error message, but the core issue is that this logic is currently ambiguous. What does it mean for a matrix to be less than a number?

---

<div class="post-metadata">

**Author:** ![Stefani](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Stefani](https://discourse.julialang.org/u/Stefani)\
**Post date:** [April 8, 2024, 8:04am UTC](https://discourse.julialang.org/t/two-conditions-in-a-while-error-typeerror-non-boolean-bitmatrix-used-in-boolean-context/112564/4 "2024-04-08T08:04:19Z")

</div>

Hi,

thank you very much for your response and advices! I did not know about the `any` and `all` functions, so thank you for that too.

Does ` while any(dR(i,j) .> 0.01) || any(dW(i,j) .> 0.01)`  
mean as long as there are any elements for which dR(i,j) \> 0.01 or dW(i,j) \>0.01 holds, the loop runs? If it is so, it is not what I am alooking for, because there will always be elements for which this conditions holds. Hence I need all right? But that resulted in the following error: ERROR: MethodError: no method matching (::var"#16#20")(::Matrix{Float64})

Thank you for any further comments!

---

<div class="post-metadata">

**Author:** ![Stefani](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Stefani](https://discourse.julialang.org/u/Stefani)\
**Post date:** [April 8, 2024, 8:08am UTC](https://discourse.julialang.org/t/two-conditions-in-a-while-error-typeerror-non-boolean-bitmatrix-used-in-boolean-context/112564/5 "2024-04-08T08:08:44Z")

</div>

Hallo Mike,

thank you for your answer!

I mean `dR` evalutatet at the point (i,j) which coincides with the location ij.  
I don`t want the matrix to be less than a scalar, I am looking for an element in the matrix, i.e. the point at which the evaluation of `dR` and `dW` is less than any \epsilon. I hope I made myself clear now.  
Thank you for any further ideas.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [April 8, 2024, 10:41am UTC](https://discourse.julialang.org/t/two-conditions-in-a-while-error-typeerror-non-boolean-bitmatrix-used-in-boolean-context/112564/6 "2024-04-08T10:41:19Z")

</div>

Let `A` be some m\times n matrix. The statement `A < epsilon` is undefined because you’re trying to compare a matrix to a scalar. The dot you used (`.<`) is for broadcasting and indicates that you want this comparison to run for every element. So, `A .< epsilon` returns a new matrix of Boolean values indicating whether the corresponding elements in matrix A satisfied the condition.

If you only want to check a particular element you can use index notation: `A[i, j]` returns the element at index ij.

If you want to check all of the elements and then reduce those results with some extras logic, you can also do that. The `any` and `all` functions are examples of reductions.
