Calculate mean from a cell array.

24 visualizaciones (últimos 30 días)
Rajvi Amle
Rajvi Amle el 16 de Jul. de 2021
Editada: Rik el 18 de Jul. de 2021
I am trying to calculate the mean from 1x10 cell array where the array contains matrices of 138x1 dimensions. But the dimensions of all 10 arrays are not the same. So is it possible to calculate mean out of all together the cell arrays having different dimensions? Here I am attaching the file Sum_col.mat from which I want to calculate the mean.
Any help would be really appreciated. Thank you in advance.

Respuesta aceptada

Rik
Rik el 16 de Jul. de 2021
Two options of what you could mean:
%load your data first
websave('data.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/686468/Sum_col.mat');
Sum_col=load('data.mat');Sum_col=Sum_col.Sum_col
Sum_col = 1×10 cell array
{135×1 double} {138×1 double} {136×1 double} {136×1 double} {133×1 double} {136×1 double} {136×1 double} {135×1 double} {137×1 double} {136×1 double}
%mean of each cell (returning a 1x10 array)
cellfun(@mean,Sum_col)
ans = 1×10
0.0785 0.0792 0.0773 0.0814 0.0712 0.0781 0.0716 0.0701 0.0790 0.0763
%mean of every element (returning a 138x1 array)
tmp=Sum_col;max_sz=max(cellfun('prodofsize',tmp));
for n=find(cellfun('prodofsize',tmp)<max_sz)
tmp{n}((end+1):max_sz)=NaN; % fill extra entries with NaN
end
mean(cell2mat(tmp),2,'omitnan')
ans = 138×1
0 0.0004 0.0007 0.0011 0.0029 0.0046 0.0064 0.0080 0.0109 0.0137
  2 comentarios
Rajvi Amle
Rajvi Amle el 17 de Jul. de 2021
@Rik Thank you so much for your response and help. Further based on this mean calculation, I tried to calculate standard deviaion and confidence interval as:
%mean of every element (returning a 138x1 array)
tmp=Sum_col;max_sz=max(cellfun('prodofsize',tmp));
for n=find(cellfun('prodofsize',tmp)<max_sz)
tmp{n}((end+1):max_sz)=NaN; % fill extra entries with NaN
end
mean(cell2mat(tmp),2,'omitnan')
std=col_mean/size(tmp,2); % standard deviation
CI95 = mean + 1.96.*(std/sqrt(length(tmp))) %confidence interval
So i just wanted to confirm that in standard deviation calculation, I should take size of tmp as 'size(tmp,2)' or size(max_sz,2)? Could you please help me giving any suggestions?
Rik
Rik el 18 de Jul. de 2021
Editada: Rik el 18 de Jul. de 2021
I would suggest using the std function, instead of calculating it yourself. The std function also has an omitnan flag.
If you want to calculate the number of elements yourself, you can use the sum of the result of the isnan function.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre NaNs en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by