2016-01-22 57 views
1

我有一個表,這是類似這樣的:如何從Matlab中的表中刪除特定列中包含NaN的行?

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  

我需要刪除具有楠列1行。其他所有行中可能有或沒有NaN的行應保留。所以期望的輸出應該是這樣的:

Table2 = 

     Column1 Column2 Column3 
     _______ _______ _______ 

Row2  1   5  NaN  
Row3  2  NaN   4  

當然,這只是一個簡單的例子。真正的表是巨大的,我將一次處理一列,這就是爲什麼我需要有選擇地刪除特定列中包含NaN的行。

有沒有辦法做到這一點,而無需將錶轉換爲結構數組或其他東西?

+1

'Table1(isnan(Table1 {:,1}),:)= []'? – Geoff

+0

@Geoff這是我試圖做的,並沒有產生與此功能的另一個表。我的Table1包含我打算反覆使用的原始數據,所以我需要將輸出保存爲Table2。我覺得它應該很簡單,但我無法弄清楚... –

回答

4

我嘗試這樣做:

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

我使用的是:第一列稱爲列1。

注意Table1.Column1返回:

ans = 

    NaN 
    1 
    2 

等選擇該列中的非NaN值是通過使用〜isnan實現()。

其餘的是純粹索引到表中。我用上面的命令獲得以下內容:

Table2 = 

     Column1 Column2 Column3 
     _______ _______ _______ 

Row2 1   5  NaN  
Row3 2   NaN   4 
相關問題