how to calculate mean of cell arrays within cell array
4 次查看(过去 30 天)
显示 更早的评论
Dear,
I have "T_mon" which is 1X12 cell array (January to December). Each column includes another cell array (1x13), (1x12),...(1x11). For every column in 1X12 cell array, I want to get mean of T_mon{1, 1}, T_mon{1, 2} ... up to T_mon{1, 12}, to be a double within a cell 1x12. How can I do this?
This cell within the cell is coming from this piece of for loop I wrote. It might not be very clever way of doing this.
for i = 1 : 12
A = T_mon{i};
T_mean{i} = cellfun(@(x) nanmean(x,2), A,'UniformOutput',false);
end
1 个评论
Rik
2020-3-3
So what data do you want to get the mean from? Can you show a small example? Do you want to calculate the mean of all values in T_mon{1}?
回答(1 个)
Mario Malic
2020-3-3
You will need to have two loops, looping over i and j.
for i = 1:length(T_mon)
for j = 1:length(T_mon{1,i})
M(i,j) = mean(T_mon{1,i}{1,j});
end
end
I noticed that there are cells that have different sizes, so I included that with length.
3 个评论
Mario Malic
2020-3-3
编辑:Mario Malic
2020-3-3
for i = 1:length(T_mon)
for j = 1:length(T_mon{1,i})
M(i,j) = sum(T_mon{1,i}{1,j});
end
end
A = nanmean(M,2); % mean value of rows
A = A'
I hope I understood well what you wanted.
Edit: didn't know that 'nanmean' existed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!