How can you delete rows of a matrix in Matlab by some criteria?

In Matlab, how can I remove spesific rows from the required matrix? If, for example, I would like to remove all rows from a matrix that contain a spesific value (like 0 or NaN)?

+3


source to share


1 answer


Let's say you have A

A = [1 2 3;4 5 0; 7 8 9; 10 NaN 12]

A =

     1     2     3
     4     5     0
     7     8     9
    10   NaN    12

      

Then you can select rows like this:

any(isnan(A'))

ans =

     0     0     0     1

      



To remove these NaN

-containing lines, you can do:

A(any(isnan(A')),:) = []

A =

     1     2     3
     4     5     0
     7     8     9

      

You can select 0

-containing lines any(A' == 0)

. If you want all elements to be 0

or NaN

s you can use all

instead any

.

+4


source







All Articles