Clear Filters
Clear Filters

how can i get proper ans to following code.

1 view (last 30 days)
it is giving output as NaN. What should I do to solve this problem? I have attached code and cell file.

Answers (2)

Guillaume
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.

Walter Roberson
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()

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!