how to calculate mean of cell arrays within cell array

조회 수: 3 (최근 30일)
Farshid Daryabor
Farshid Daryabor 2020년 3월 3일
편집: Mario Malic 2020년 3월 3일
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
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
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
Farshid Daryabor
Farshid Daryabor 2020년 3월 3일
It is what I expacted
for jj = 1 : length(T_mon)
A = T_mon{jj};
N = cellfun(@numel, A);
M = max(N);
A_new = cell2mat(cellfun(@(x) [x; nan(M - numel(x),1)],A,'uni',0));
B = nanmean(A_new,2);
C{jj}=B;
end
Mario Malic
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by