More of a general MATLAB question than looking for programming advice — if I have:
y = cellfun(@(x)sum(x(:)), Z, 'un', 0);
where there are a combinations of NaN‘s and real numbers in each cell matrix, when I sum all elements of those matrices per cell, will I always get total = NaN because there are NaN‘s in there, or will they be ignored and just sum the real numbers. The reason I ask is because I am getting:
y = [NaN] [NaN] [NaN]
[NaN] [NaN] [NaN]
[NaN] [NaN] [NaN]
an example cell matrix (cell element) would be:
x{1,1} = NaN 2 3
4 5 6
7 8 9
so I would expect the first element of y to be:
y{1,1} = 44
How am I not getting this?
To ignore the NaNs, just use vector indexing in your anonymous function, by replacing the colon (
:)with
~isnan(x):So you get: