Removing NAN values from the table and deleting it.
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (2)
ahmed nebli
il 17 Nov 2018
1 voto
use this : (isnan(X)) = [] % X is the table
5 Commenti
Walter Roberson
il 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
il 18 Nov 2018
Modificato: madhan ravi
il 18 Nov 2018
madhan ravi
il 18 Nov 2018
@Shelender see my comment in the above answer it will give you the desired result you want
Talha Idrees
il 2 Mag 2021
not works
Walter Roberson
il 3 Mag 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 Commenti
Walter Roberson
il 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),:)
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!