anyDetermine if any element is non-zero.
b = any(A)A is a scalar, b is logical 1 if A is non-zero, and logical 0 if otherwise.A is a vector, b is logical 1 if at least one element of A is non-zero, and logical 0 if otherwise.A is a matrix or an array of ndims(A) >= 3, an element of b is assigned logical 1 if at least one element of corresponding vector of A along the first non-singleton dimension is non-zero, and logical 0 if otherwise. any(A) collapses the first non-singleton dimension of A. That means, if size(A) is [s1, s2, ..., sN], then size(b) is equal to [1, s2, s3, ..., sN].b = any(A, k)k should be a real positive integer scalar.b is assigned logical 1 if any element of the corresponding vector of A along the k-th dimension is non-zero, and logical 0 if otherwise.NaN present in A. This is equivalent to replacing all NaN values in A by 0. As a result, any(NaN) gives 0.A is a character array, it is first converted to a double array.any(A) and any(A, k) always return an empty array when A is empty.a has any non-zero elements.a=randi(4,3,3,3) - 2
any(a,3)
a(:, :, 1) =
-1.000 0.000 -1.000
2.000 2.000 0.000
0.000 -1.000 -1.000
a(:, :, 2) =
1.000 0.000 -1.000
1.000 2.000 1.000
0.000 1.000 2.000
a(:, :, 3) =
0.000 0.000 1.000
-1.000 1.000 1.000
1.000 0.000 2.000
ans =
1 0 1
1 1 1
1 1 1
k > ndims(A), calling any(A, k) is the same as performing any() to each of the elements of A.r=rand(2, 3, 4);
% Creating the array rr with some zeros.
rr = r.*(r>0.5)
any(rr, 4)
rr(:, :, 1) = 1e-1 ×
5.9314 8.6059 0.0000
5.3948 0.0000 0.0000
rr(:, :, 2) = 1e-1 ×
8.5716 7.0417 5.3465
0.0000 0.0000 8.6345
rr(:, :, 3) = 1e-1 ×
6.9417 9.0411 8.1402
0.0000 0.0000 0.0000
rr(:, :, 4) = 1e-1 ×
5.2895 6.0550 6.0822
0.0000 7.0642 5.7643
ans(:, :, 1) =
1 1 0
1 0 0
ans(:, :, 2) =
1 1 1
0 0 1
ans(:, :, 3) =
1 1 1
0 0 0
ans(:, :, 4) =
1 1 1
0 1 1