how can i get proper ans to following code.
1 次查看(过去 30 天)
显示 更早的评论
it is giving output as NaN. What should I do to solve this problem? I have attached code and cell file.
0 个评论
回答(2 个)
Guillaume
2018-7-8
编辑:Guillaume
2018-7-8
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 个评论
Walter Roberson
2018-7-8
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!