How to remove NaN cells inside a structure with several cell arrays inside?
Show older comments
I have a cell struct
data 49x26 cell
and each cell inside this struct corresponds to
1×1 cell array {64×250 single}
This cell arrays are composed with numbers, but from time to time a NaN value shows up and I need to eliminate it, not just put it into 0, I wanto to permanently eliminate it. How can i do this?
Answers (1)
You can't remove the NaN values without changing the shape of the data. You could replace the NaN values with a different place holder but if you remove them, the 64x250 shape will change. Usually the size of matrices means something (e.g. number of observations, conditions, groups, etc). But after you remove the NaN values, you lose that information.
Nevertheless, there's how to remove the NaN values. Assuming the NaNs have no pattern, the result will be a row vector of non-nan values and the length of the row is determined by the number of non-nan values.
% Create fake data
C = cell(49,26);
for i = 1:numel(C)
x = round(rand(64,250),1);
C{i} = {x./x};
end
% Show sample of data
C
C{1}
% Function to count total number of NaNs
nanCountFcn = @(C)sum(isnan(cell2mat([C{:}])), 'all');
nanCountFcn(C) % number of NaN values
% Delete NaN values
% All 64x250 arrays will become row vectors
% of various lengths depending on the number of
% NaNs removed
for i = 1:numel(C)
C{i}{:}(isnan(C{i}{:})) = [];
end
% Count remaining NaNs
nanCountFcn(C)
% Show sample of updated data
C
C{1}
6 Comments
Melissa Jones
on 8 Nov 2021
Adam Danz
on 8 Nov 2021
Can you confirm that your data look like mine? Specifically,
C should be a 49×26 cell array
C{1} should be a 1×1 cell array containing {64×250 double}
Melissa Jones
on 8 Nov 2021
Melissa Jones
on 8 Nov 2021
Edited: Melissa Jones
on 8 Nov 2021
Adam Danz
on 8 Nov 2021
I believe my demo data fits the description in your question but it doesn't seem to match your data. Could your provide an explicit example of your data?
Melissa Jones
on 8 Nov 2021
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!