简体   繁体   中英

How can you remove matrix rows in Matlab based on some criteria?

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

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 choose the rows as follows:

any(isnan(A'))

ans =

     0     0     0     1

To delete those NaN -containing rows, you can do:

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

A =

     1     2     3
     4     5     0
     7     8     9

You can choose 0 -containing rows by any(A' == 0) . If you want all elements to be 0 s or NaN s, then you can use all instead of any .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM