7

I know I can use isnan to check for individual elements, such as

for i=1:m
    for j=1:n
        if isnan(A(i,j))
            do something
        end
    end
end

However, instead what I want to do is

 if any(isnan(A))
      do something
 end

When I tried to do this, it doesn't go into the argument because it is considered false. If I just type any(isnan(A)), I just get 1 0 1. So how do I do this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Niseonna
  • 179
  • 2
  • 3
  • 13

1 Answers1

8
any(isnan(A(:)))

Since A was a matrix, isnan(A) is also a matrix and any(isnan(A)) is a vector, whereas the if statement really wants a scalar input. Using the (:) notation flattens A into a vector, regardless of the initial size.

Pursuit
  • 12,285
  • 1
  • 25
  • 41