Array – How to use any () on a multidimensional array?

I’m testing an arbitrarily large, arbitrarily sized logical array, and I want to know if one or more of them are true. any() can only be in one dimension at a time Works, so does sum(). I know I can test the number of dimensions and repeat any () until I get an answer, but I want a faster, more frank, and more elegant method.

Ideas?

I am running 2009a (R17, in old terms, I think).

If your data is in matrix A, please try the following:

anyAreTrue = any(A(:));

Edit: To explain people who are not familiar with the syntax, A(:) uses the colon operator to get the entire contents of the array A, regardless of the size, and reshape them into a single column vector (size numberl) (A)×1). The operation on the result column vector only needs to call ANY once.

I am testing an arbitrarily large and arbitrarily sized logical array, and I want to know if there is one of them or Multiple is true. any() can only work in one dimension at a time, so does sum(). I know I can test the number of dimensions and repeat any() until I get an answer, but I want one more Faster, more frank, and more elegant method.

Ideas?

I am running 2009a (R17, in the old parlance, I think).

If your data is in matrix A, Please try the following:

anyAreTrue = any(A(:));

Edit: To explain people who are not familiar with the grammar, A (:) Use the colon operator to get the entire contents of the array A, regardless of the size, and reshape them into a single column vector (size numberl) (A)×1). The operation on the result column vector only needs to call ANY once.

Leave a Comment

Your email address will not be published.