フィルターのクリア

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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by