How can I remove columns that contain only NaNs from a cell array?

2 views (last 30 days)
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
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) = [];

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!