how can i get proper ans to following code.
1 view (last 30 days)
Show older comments
it is giving output as NaN. What should I do to solve this problem? I have attached code and cell file.
0 Comments
Answers (2)
Guillaume
on 8 Jul 2018
Edited: Guillaume
on 8 Jul 2018
Considering that your cell array only contains one matrix I don't see why you're bothering with a cell and all these cellfun.
Anyway, the problem is simple: columns 6987 to 7984 of that matrix are just NaNs, so of course when you sum across the columns you get NaN. Possibly you could fix that by adding the 'omitnan' option to your sums but most likely the proper fix is for you to find out why there are NaNs in the first place.
Note that even if there were more than one matrix in your cell array, your comp_gm_fv could be simplified. The first cellfun doesn't need 'UniformOutput', false and there's no point in transposing a matrix before summing across the columns, simply sum across the rows instead:
function [gm, gv] = comp_gm_gv(data)
% computes the global mean and variance of data
nframes = sum(cellfun(@(x) size(x, 2), data));
gm = cellfun(@(x) sum(x, 2), data, 'UniformOutput', false);
%gm = cellfun(@(x) sum(x, 2, 'omitnan'), data, 'UniformOutput', false);
gm = sum(cell2mat(gm), 1)/nframes;
gv = cellfun(@(x) sum(bsxfun(@minus, x, gm).^2, 2), data, 'UniformOutput', false);
%gv = cellfun(@(x) sum(bsxfun(@minus, x, gm).^2, 2, 'omitnan'), data, 'UniformOutput', false);
gv = sum(cell2mat(gv), 1)/( nframes - 1 );
end
Commented lines are with the 'omitnan' option.
0 Comments
Walter Roberson
on 8 Jul 2018
About 9% of your data is nan, and there is a nan in every row. When you sum() values that contain nan, the result is nan.
Perhaps you want to use the 'omitnan' option of sum()
0 Comments
See Also
Categories
Find more on NaNs 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!