Removing NAN values from the table and deleting it.
Show older comments
Hi
I have a table which is arrranged in susch a waym that it has one row of data and other row which contain NAN and so on, I want to get rid of NAN and aferwards deleting it.
Could you help me with this.
Accepted Answer
More Answers (2)
ahmed nebli
on 17 Nov 2018
1 vote
use this : (isnan(X)) = [] % X is the table
5 Comments
Walter Roberson
on 17 Nov 2018
isnan() cannot be applied directly to a table. It can be applied to content of a table. In the case of a table that contains only numeric scalars you could do
X(any(isnan(X{:,:}),2),:) = [];
but you are probably better off using the new rmmissing() that madhan ravi mentions.
Shelender Kumar
on 18 Nov 2018
Edited: madhan ravi
on 18 Nov 2018
madhan ravi
on 18 Nov 2018
@Shelender see my comment in the above answer it will give you the desired result you want
Talha Idrees
on 2 May 2021
not works
Walter Roberson
on 3 May 2021
not works
Magik 8-Ball says:
Concentrate and ask again
Following another question I found this code working really good:
Xnew=X((isfinite(X)));
The new array has no Nan inside.
5 Comments
Walter Roberson
on 12 Feb 2025
This will not work for table objects.
Also, for 2D (or more) arrays, it has the side effect of unraveling the results into a column vector.
It will work in this way, assuming that you have a X,Y,Z table you can delete the rows with NaN in X for example in this way:
T=[1,2,3;nan,2,3;1,2,3;1,2,3;nan,2,3]
X=T(:,1) ; Y=T(:,2); Z=T(:,3);
Xnew=X((isfinite(X)));
Ynew=Y((isfinite(X)));
Znew=Z((isfinite(X)));
T_new = [Xnew,Ynew,Znew]
You will have a table that doesn't have any NaN value in X
The question is about table objects.
T = array2table([1,2,3;nan,2,3;1,2,3;1,2,3;nan,2,3])
X=T(:,1) ; Y=T(:,2); Z=T(:,3);
Xnew=X((isfinite(X)));
Ynew=Y((isfinite(X)));
Znew=Z((isfinite(X)));
T_new = [Xnew,Ynew,Znew]
Yes the principle is the same, if you have a table, for example in your case, you can call the columns using T.Column_name, in this case:
T = array2table([1,2,3;nan,2,3;1,2,3;1,2,3;nan,2,3])
X=T.Var1 ; Y=T.Var2; Z=T.Var3;
Xnew=X((isfinite(X)));
Ynew=Y((isfinite(X)));
Znew=Z((isfinite(X)));
T_new = array2table([Xnew,Ynew,Znew])
T = array2table([1,2,3;nan,2,3;1,2,3;1,2,3;nan,2,3])
T_new = T(isfinite(T.Var1),:)
Since the question only asked about NaNs, isnan might be a better function to use than isfinite, i.e.
T_new = T(~isnan(T.Var1),:)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!