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

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.

Here you go. :slight_smile:

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

3 Likes

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

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

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
5 Likes

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:

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> @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> 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>  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)

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:

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

2 Likes

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.

8 Likes

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

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

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

julia> 
3 Likes