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?
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
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