how can i get proper ans to following code.

조회 수: 1 (최근 30일)
Siddhesh Karbhari
Siddhesh Karbhari 2018년 7월 8일
답변: Walter Roberson 2018년 7월 8일
it is giving output as NaN. What should I do to solve this problem? I have attached code and cell file.

답변 (2개)

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

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

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by