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.
NA
except the date
)?close
has data (a number)?close
is a nonzero number. 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:
mytable = rmmissing(mytable);
and note that this also works with arrays as per official documentation:
R = rmmissing(A) removes missing entries from an array or table. If A is a vector, then rmmissing removes any entry that contains missing data. If A is a matrix or table, then rmmissing removes any row that contains missing data. Missing values are defined according to the data type of A:
NaN - double, single, duration, and calendarDuration
NaT - datetime
'missing' - string
'undefined' - categorical
' ' - char
{''} - cell of character arrays
If you are working with an earlier version, go for:
mytable = mytable(~any(ismissing(mytable),2),:);
A good multi-purpose tutorial to read on this argument can be found here.