Removing NAN values from the table and deleting it.

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

rmmissing(T) %deletes row containing nan where T your table

16 Comments

According to your comment in the answer below this would give you the desired result:
T=table2array(T)
T(sum(isnan(T),2)==size(T,2),:)=[]
%OR
T(sum(isnan(T{:,:}),2)==size(T{:,:},2),:)=[] % in one step
Thanks but It does not gives me what I wanted, as a example my data will look like below
NaN NaN NaN NaN
1.38E-01 -1.02E-05 -8.81E-02 -1.08E-05
1.54E-01 NaN -5.51E-02 NaN
6.41E+01 7.83E-06 1.09E+02 3.83E-05
NaN NaN NaN NaN
and answer should look like below
answer
1.38E-01 -1.02E-05 -8.81E-02 -1.08E-05
1.54E-01 NaN -5.51E-02 NaN
6.41E+01 7.83E-06 1.09E+02 3.83E-05
madhan ravi's reply: see the below comment it gives exactly the same result you want!
>>
format shorte
t=[NaN NaN NaN NaN
1.38E-01 -1.02E-05 -8.81E-02 -1.08E-05
1.54E-01 NaN -5.51E-02 NaN
6.41E+01 7.83E-06 1.09E+02 3.83E-05
NaN NaN NaN NaN ];
T=table(t);
T=table2array(T)
T(sum(isnan(T),2)==size(T,2),:)=[]
T =
NaN NaN NaN NaN
1.3800e-01 -1.0200e-05 -8.8100e-02 -1.0800e-05
1.5400e-01 NaN -5.5100e-02 NaN
6.4100e+01 7.8300e-06 1.0900e+02 3.8300e-05
NaN NaN NaN NaN
T =
1.3800e-01 -1.0200e-05 -8.8100e-02 -1.0800e-05
1.5400e-01 NaN -5.5100e-02 NaN
6.4100e+01 7.8300e-06 1.0900e+02 3.8300e-05
>>
Thanks a lot but it does not work on my data file somehow
Anytime :) ,it worked see the text file attached after running the code
Thanks my mistake, I did not look carefully enough
could you help me understand this line, I do know about isnan but rest is not clear to me, thanks
T(sum(isnan(T),2)==size(T,2),:)=[]
Ah I am bad at explaining things let me give it a try. isnan returns logical index which is summed across rows , so when the sum equals the column numbers of the matrix in which it's your table the entire row is deleted.
Thanks, but I did not understand this part, could you explain me
size(T,2),:)
size(T,2) gives you the number of columns in T
see size , matrix indexing and logical indexing to better understand what's going on
You're welcome , respond to your previous question in my comment
sum(isnan(T),2)==size(T,2)
is better expressed as
all(isnan(T),2)
Thank you sir Walter , have to familiarise with any and all it‘s slightly confusing :)

Sign in to comment.

More Answers (2)

use this : (isnan(X)) = [] % X is the table

5 Comments

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.
Thanks , But I need something like this, let us say raw data look like this
NaN NaN NaN NaN
1.38E-01 -1.02E-05 -8.81E-02 -1.08E-05
1.54E-01 NaN -5.51E-02 NaN
6.41E+01 7.83E-06 1.09E+02 3.83E-05
NaN NaN NaN NaN
answer
1.38E-01 -1.02E-05 -8.81E-02 -1.08E-05
1.54E-01 NaN -5.51E-02 NaN
6.41E+01 7.83E-06 1.09E+02 3.83E-05
@Shelender see my comment in the above answer it will give you the desired result you want
not works
Magik 8-Ball says:
Concentrate and ask again

Sign in to comment.

Following another question I found this code working really good:
Xnew=X((isfinite(X)));
The new array has no Nan inside.

5 Comments

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]
T = 5×3
1 2 3 NaN 2 3 1 2 3 1 2 3 NaN 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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]
T_new = 3×3
1 2 3 1 2 3 1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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])
T = 5x3 table
Var1 Var2 Var3 ____ ____ ____ 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)));
Incorrect number or types of inputs or outputs for function isfinite.
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])
T = 5x3 table
Var1 Var2 Var3 ____ ____ ____ 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_new = 3x3 table
Var1 Var2 Var3 ____ ____ ____ 1 2 3 1 2 3 1 2 3
T = array2table([1,2,3;nan,2,3;1,2,3;1,2,3;nan,2,3])
T = 5x3 table
Var1 Var2 Var3 ____ ____ ____ 1 2 3 NaN 2 3 1 2 3 1 2 3 NaN 2 3
T_new = T(isfinite(T.Var1),:)
T_new = 3x3 table
Var1 Var2 Var3 ____ ____ ____ 1 2 3 1 2 3 1 2 3
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),:)
T_new = 3x3 table
Var1 Var2 Var3 ____ ____ ____ 1 2 3 1 2 3 1 2 3

Sign in to comment.

Asked:

on 17 Nov 2018

Edited:

on 12 Feb 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!