How can I remove columns that contain only NaNs from a cell array?
2 views (last 30 days)
Show older comments
MathWorks Support Team
on 10 Jun 2014
Answered: MathWorks Support Team
on 10 Jun 2014
I have a cell array that contains NaNs. Some columns only contain NaNs. How can I detect these columns and remove them from the cell array?
Accepted Answer
MathWorks Support Team
on 10 Jun 2014
% Create some data
A = {2, NaN, 3; NaN, NaN, 'text'; 5, NaN, 6};
% Create an anonymous function to detect NaNs
f = @(x) any(isnan(x));
% Detect where there are NaNs
B = cellfun(f, A);
% Now detect columns that contain ONLY NaNs
C = all(B);
% Remove these columns
A(:,C) = [];
0 Comments
More Answers (0)
See Also
Categories
Find more on Cell Arrays 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!