So again, numbered variables and field names are a bad idea. (see here and here) for the related questions). The whole thing would be simpler if the original cell array was used. If the original cell array had been kept, the code over the 3 questions could be simplified to:
precip_monthly = cell(size(precips));
for idx = 1:numel(precips)
if size(precips{idx}, 3) == 365
month_lengths=[31 28 31 30 31 30 31 31 30 31 30 31];
else
month_lengths=[31 29 31 30 31 30 31 31 30 31 30 31];
end
p_month =mat2cell(precips{idx}, size(precips{idx}, 1), size(precips{idx}, 2), month_lengths);
p_month = cellfun(@(x) sum(x, 3, 'omitnan'), p_month, 'UniformOutput', false);
precip_monthly{idx} = cell2mat(p_month);
end
Note that you have written a comment that says find mean for each month but you use the sum function. So either the code or the comment is wrong.
Best Answer