如何根据某些条件在Matlab中删除矩阵行? [英] How can you remove matrix rows in Matlab based on some criteria?

查看:685
本文介绍了如何根据某些条件在Matlab中删除矩阵行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中,如何从所需的矩阵中删除特殊的行?例如,如果我想从矩阵中删除所有包含特殊值(例如0或NaN)的行?

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)?

推荐答案

假设您有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

要删除那些包含NaN的行,您可以执行以下操作:

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

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

A =

     1     2     3
     4     5     0
     7     8     9

您可以通过any(A' == 0)选择包含0的行.如果希望所有元素都是0NaN s,则可以使用all代替any.

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

这篇关于如何根据某些条件在Matlab中删除矩阵行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆