我将所有数据放在一个巨大的表格中,现在正努力尝试使用它.我需要删除在几个特定列中具有NaN的行.此外,该表位于数组内.这是一个简化版本:
Col1 = [ 1; 1; 1; NaN];
Col2 = [ 1; 1; NaN; NaN];
Col3 = [NaN; NaN; NaN; 1 ];
Col4 = [ 1; 1; NaN; NaN];
Rows = {'Row1'; 'Row2'; 'Row3'; 'Row4'};
T = table(Col1, Col2, Col3, Col4, 'RowNames', Rows) %this is the table
data(1).tables = T %this is the array that contains the table
Run Code Online (Sandbox Code Playgroud)
该表如下所示:
Col1 Col2 Col3 Col4
____ ____ ____ ____
Row1 1 1 NaN 1
Row2 1 1 NaN 1
Row3 1 NaN NaN NaN
Row4 NaN NaN 1 NaN
Run Code Online (Sandbox Code Playgroud)
我需要删除第2列到第4列中包含所有NaN的所有行.因此,在此示例中,应删除Row3,输出将如下所示:
Col1 Col2 Col3 Col4
____ ____ ____ ____
Row1 1 1 NaN 1
Row2 1 1 NaN 1
Row4 NaN NaN 1 NaN
Run Code Online (Sandbox Code Playgroud)
在真实的表中有很多列,所以我需要在列中删除带有NaN的行,比如130到160.所以用逗号列出它们是不可取的.但是,需要检查NaN的所有列都是相邻的,所以理想情况下它应该指定一系列列,例如:data(1).tables2 = data(1).tables(~isnan(data(1).tables(:,2:4), :)),这不起作用.有谁知道我怎么能完成它?
我建议使用以下单行解决方案:
T(all(isnan(T{:,2:4}),2),:) = []
Run Code Online (Sandbox Code Playgroud)
这给出了美味的结果:
T =
Col1 Col2 Col3 Col4
____ ____ ____ ____
Row1 1 1 NaN 1
Row2 1 1 NaN 1
Row4 NaN NaN 1 NaN
Run Code Online (Sandbox Code Playgroud)