What is the most elegant way to reduce elementwise-conditions in multidimensional-arrays to one logical variable in Matlab? I need this for a big project with a lot of if conditions and assertions. In the Matlab documentation about logical arrays and find array elements there is no satifying solution for this problem.
For example, a logical variable myBool
is true iff there are two ones at the same position in the matrices A
and B
:
A = [0,1;0,0]
B = [0,1;1,0]
My preferred solution so far is:
myBool = any(A(:)==1 & B(:)==1)
But it doesn't look like the shortest solution and it doesn't work with array indexing.
A shorter but not very readable solution:
myBool = any(A(B==1))
The biggest problem is that for higher dimensional arrays the functions like nnz() only reduce the order by one dimension without the colon (:), but with the colon it is not possible to index a part of the array...