How do I remove all the rows with NaN-value of a specific column?
78 views (last 30 days)
Show older comments
Hey, I want to delete all the rows with NaN value of my first column using the 'isnan'-function. The matrix is 570x9.
After I do that I need to delete all columns of the matrix with >25% NaNs, also using the 'isnan'-function.
Thanks in advance for the answers! I'm sure the solution is really basic but I can't get my head around it.
0 Comments
Accepted Answer
dpb
on 21 Nov 2021
X(isnan(X(:,1)),:)=[]; % solution a -- remove those rows w/ NaN in column 1
X=X(~isnan(X(:,1)),:); % solution b -- keep only those rows NOT NaN in column 1
X=X(isfinite(X(:,1)),:); % solution c -- keep only those rows NOT NaN|Inf in column 1
NB: Solution C is a different result than either A or B IFF (if and only if) column one also might contain Inf values besides NaN.
X=X(~isnan(X(:,1)),:);
isGood=sum(isnan(X))<=(size(X,1)/4);
X=X(:,isGood);
2 Comments
More Answers (0)
See Also
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!