In Matlab, how do I drop rows with all NAs or with some NAs in the data.
I have a table with columns date open high low close volume
The date exists in all the rows.
Some of the rows have all other columns besides the date with NA.
Some rows have all information ok except the close.
- How do I get a table without the "NA" rows (everything
NA
except thedate
)? - How do I get a table with only the rows that
close
has data (a number)? - How do I get a table with only the rows that
close
is a nonzero number. - Conversly, how do I get a table with only the rows where everything is good except the
close
row having an NA?
I saw this in Matlab documentation for a matrix:
>> X = [10; 0.04500; 0; NaN; NaN];
>>X(isnan(X)) = []
ans = [10; 0,04500, 0]
And this for all values being NAN (which is never my case because the date 'index' column is always valid...)
A(~any(~isnan(A), 2),:)=[];
Which if I understand correctly means in pseudo code:
all in matrix A(hasno(numeric(A), INCOLUMNS), FORALLCOLUMNS) =? [];
Which results in all rows WITH at least one numeric in one of the columns.
...But is there anything like it for a table, and for checking only the data fields?
If you are working with
Matlab R2016b
or greater, all you have to do in order to remove table rows with missing values is:and note that this also works with arrays as per official documentation:
If you are working with an earlier version, go for:
A good multi-purpose tutorial to read on this argument can be found here.