如何从Matlab中的表中删除包含特定列中的NaN的行?

时间:2016-01-22 03:02:29

标签: matlab

我有一张表,与此类似:

Rows = {'Row1';'Row2';'Row3'};
Column1 = [NaN;1;2];
Column2 = [4;5;NaN];
Column3 = [NaN;NaN;4];
Table1 = table(Column1,Column2,Column3,...
'RowNames',Rows)

Table1 = 

        Column1    Column2    Column3
        _______    _______    _______

Row1    NaN          4        NaN    
Row2      1          5        NaN    
Row3      2        NaN          4    

我需要删除Column1中具有NaN的行。在其他列中可能有也可能没有NaN的所有其他行应该保留。所以期望的输出应该如下所示:

Table2 = 

        Column1    Column2    Column3
        _______    _______    _______

Row2      1          5        NaN    
Row3      2        NaN          4    

当然,这只是一个简化的例子。实际表格很大,我将一次使用一列,这就是为什么我需要有选择地删除特定列中包含NaN的行。

有没有办法在不将表转换为结构数组或其他内容的情况下执行此操作?

1 个答案:

答案 0 :(得分:4)

我试过了:

Table2 = Table1(~isnan(Table1.Column1), :)

我利用了第一列名为Column1的事实。

请注意,Table1.Column1返回:

ans =

   NaN
     1
     2

所以选择此列中的非NaN值是通过使用~isnan()来实现的。

其余的纯粹是索引到表中。我使用上面的命令得到以下内容:

Table2 = 

        Column1    Column2    Column3
        _______    _______    _______

Row2    1            5        NaN    
Row3    2          NaN          4