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

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

This?

all(x -> x > 0, RR)
1 Like

Thank you very much!

1 Like

Or

all(>(0), RR) 
3 Likes

When I do the below comparison:

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 matrixabs.( 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.

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:

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

Use a generator:

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.

1 Like

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

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

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

Very nice yes, they are faster

Thank you very much for your feedback.