# How do you do the Matlab equivilant of any,all in Julia

**URL:** <https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289>\
**Category:** General Usage\
**Created:** [January 20, 2023, 11:31pm UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289 "2023-01-20T23:31:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![m\_x155](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@m\_x155](https://discourse.julialang.org/u/m_x155)\
**Post date:** [January 20, 2023, 11:31pm UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/1 "2023-01-20T23:31:23Z")

</div>

In matlab any and all allow you to quickly identify rows or columns of a matrix that meet some criteria.  
For example if you have an array of logicals such as  
A=[1 1;0 1;0 0; 0 1; 1 1]  
any(A,2) checks if there are any “true” values on each row and would return answer=[true;true;false;true;true]  
I know you can do this in Julia but what are the methods that do this?

Thanks.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [January 20, 2023, 11:42pm UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/2 "2023-01-20T23:42:56Z")

</div>

Here you go. 🙂

```julia
julia> a = rand(3,3) .< 0.5
3×3 BitMatrix:
 1 0 1
 1 1 1
 0 1 0

julia> all(a,dims=1)
1×3 BitMatrix:
 0 0 0

julia> all(a,dims=2)
3×1 BitMatrix:
 0
 1
 0

julia> any(a,dims=1)
1×3 BitMatrix:
 1 1 1

julia> any(a,dims=2)
3×1 BitMatrix:
 1
 1
 1

julia> count(a,dims=1)
1×3 Matrix{Int64}:
 2 2 2

julia> count(a,dims=2)
3×1 Matrix{Int64}:
 2
 3
 1

```

---

<div class="post-metadata">

**Author:** ![m\_x155](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@m\_x155](https://discourse.julialang.org/u/m_x155)\
**Post date:** [January 20, 2023, 11:55pm UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/3 "2023-01-20T23:55:23Z")

</div>

Great. Thanks. As simple as that is a google search was not straightway yielding the answer.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 20, 2023, 11:55pm UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/4 "2023-01-20T23:55:44Z")

</div>

Note that you can also eliminate the separate construction of the boolean matrix `a` with e.g.:

```julia
julia> X = rand(3,3)
3×3 Matrix{Float64}:
 0.739814 0.724324 0.140468
 0.299887 0.511811 0.93329
 0.406284 0.0990977 0.493384

julia> all(<(0.5), X, dims=1)
1×3 Matrix{Bool}:
 0 0 0

julia> all(<(0.5), X, dims=2)
3×1 Matrix{Bool}:
 0
 0
 1

julia> any(<(0.5), X, dims=1)
1×3 Matrix{Bool}:
 1 1 1

julia> any(<(0.5), X, dims=2)
3×1 Matrix{Bool}:
 1
 1
 1

```

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [January 21, 2023, 3:37am UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/5 "2023-01-21T03:37:09Z")

</div>

It’s reasonable to habitually avoid allocation. But when benchmarking the difference in run times, I often find it is not the same as the time to allocate and construct the intermediate structure. In this case, the allocation is not the main contribution to the difference in run times:

```julia
X = rand(3,3)
b = X .< 1/2 # construct a BitMatrix

function do_test(m, func::F=identity) where F
    return (all(func, m, dims=1), all(func, m, dims=2),
            any(func, m, dims=1), any(func, m, dims=2))
end

test_1(m) = do_test(m .< 1/2) # construct a BitMatrix
test_2(m) = do_test(m, <(1/2)) # pass a function

```

```julia
julia> @btime test_1($X);
  586.739 ns (18 allocations: 720 bytes)

julia> @btime test_2($X);
  428.985 ns (12 allocations: 416 bytes)

julia> @btime do_test($b);
  523.715 ns (16 allocations: 608 bytes)

```

If you convert the `BitArray` to a `Matrix{Bool}` you find that the `BitArray` is responsible for most of the inefficiency.

```julia
julia> b
3×3 BitMatrix:
 0 1 0
 0 1 1
 0 1 0

julia> b_bool = Matrix{Bool}(b)
3×3 Matrix{Bool}:
 0 1 0
 0 1 1
 0 1 0

julia> @btime do_test($b_bool);
  428.276 ns (12 allocations: 416 bytes)

```

In case you do want to make a test matrix for some reason, you can do the following which makes faster code, but it’s not very nice

```julia
julia> test_3(m) = do_test([m[i,j] < 1/2 for i in 1:3, j in 1:3]);

julia> @btime test_3($X);
  472.816 ns (13 allocations: 480 bytes)

```

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [January 21, 2023, 4:13am UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/6 "2023-01-21T04:13:58Z")

</div>

> [@m\_x155](#):
>
> Great. Thanks. As simple as that is a google search was not straightway yielding the answer.

Instead of relying on a Google search, if you know the name of the function, you can learn about it’s options by typing e.g. `?all` at the REPL prompt:

```julia
help?> all

<Some preliminary documentation elided here for clarity>

all(A; dims)

  Test whether all values along the given dimensions of an array are true.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> A = [true false; true true]
  2×2 Matrix{Bool}:
   1 0
   1 1

  julia> all(A, dims=1)
  1×2 Matrix{Bool}:
   1 0

  julia> all(A, dims=2)
  2×1 Matrix{Bool}:
   0
   1

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 21, 2023, 5:03am UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/7 "2023-01-21T05:03:09Z")

</div>

We really need to get better optimization of bitarrays. They could be using really good vectorized methods in a lot of cases that would be way faster.

---

<div class="post-metadata">

**Author:** ![m\_x155](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@m\_x155](https://discourse.julialang.org/u/m_x155)\
**Post date:** [January 25, 2023, 11:34pm UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/8 "2023-01-25T23:34:25Z")

</div>

Wow, that is handy. I didn’t know thanks.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 26, 2023, 6:40am UTC](https://discourse.julialang.org/t/how-do-you-do-the-matlab-equivilant-of-any-all-in-julia/93289/9 "2023-01-26T06:40:32Z")

</div>

There’s a subtle hint about help mode in the banner:

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.8.5 (2023-01-08)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> 

```
