How to obtain mean of columns in multiple cells?

I have a cell (say "a") and it has 5X1 dimension , Class: Cell. (Shown in picture below)
Each cell contains different cells, having size 3439x72 Single.
Now, I want to obtain the mean values of
a{1,1}(1,:) , a{2,1}(1,:) , a{3,1}(1,:) , a{4,1}(1,:) , a{5,1}(1,:),...
a{1,1}(2,:) , a{2,1}(2,:) , a{3,1}(2,:) , a{4,1}(2,:) , a{5,1}(2,:),...
a{1,1}(3,:) , a{2,1}(3,:) , a{3,1}(3,:) , a{4,1}(3,:) , a{5,1}(3,:),...
...
...
...
a{1,1}(3439,:) , a{2,1}(3439,:) , a{3,1}(3439,:) , a{4,1}(3439,:) , a{5,1}(3439,:)
The final output will be one cell with size 1x1 containing mean values of matrix "a" and having dimension 3439x72 Single.
Can anyone please help. Any help will be greatly appriciated.
The picture below shows the example of matrix "a".

 Accepted Answer

% Generate data
for i=1:5
a{i} = randn(3439, 72);
end
% find means
s = zeros(size(a{1}));
for i=1:5
s = s + a{i};
end
s = s/5;
m{1} = s % if cell output is needed
m = 1×1 cell array
{3439×72 double}
% Another way
% convert the cell array to n-d arraym
b = cell2mat(a);
b = reshape(b, 3439, 72, []);
bmean = mean(b, 3, 'omitnan')
bmean = 3439×72
-0.1072 0.7186 -1.2518 0.6538 0.0450 0.2487 -0.0131 -0.5979 0.0913 0.2459 0.2499 -0.4384 0.2268 1.2775 0.4260 0.3953 1.1213 0.4292 -0.5834 -0.0036 -0.5446 -0.2942 -0.3175 -0.0058 0.3379 0.0833 -0.1570 -0.4981 0.6502 -0.3571 0.3296 -0.3576 -0.0878 0.7029 -0.3468 -0.3598 0.3431 0.2771 -0.3125 0.4491 0.2682 0.3570 0.3437 1.4581 -0.1591 0.2845 -0.0704 0.4437 0.1149 -0.5652 0.3740 0.5756 0.3369 0.2193 -0.0896 0.7258 0.1833 -0.2490 0.6524 0.3205 0.7501 -0.7461 0.0481 -0.6806 -0.0029 -0.4085 0.1619 0.3956 0.1897 -0.9844 0.3036 0.5573 0.1616 -0.5567 -0.2975 0.3156 -0.4230 -0.3315 0.2060 0.4804 -0.2614 0.3183 -0.3542 1.0261 0.2275 0.3702 0.2705 -0.1034 0.9476 0.4038 0.5143 -0.6436 0.4221 0.0232 0.6096 0.6368 -0.0515 0.6973 -0.6362 -0.5205 0.0560 -0.0415 -0.7558 0.6641 0.6799 -0.3440 0.0877 0.3927 -0.0997 -0.9058 0.4118 -0.0052 -0.1239 0.7165 -0.0513 -0.4080 0.0001 -0.0915 -0.3898 0.3817 -0.2457 -0.3139 -0.0633 -0.2184 0.3175 0.6855 0.1546 0.1740 0.3062 0.2652 0.0356 0.4685 -0.6416 0.0628 -0.0215 0.1770 -0.1564 -0.1714 0.5383 -0.0258 1.0216 -0.2419 0.0912 -0.6164 0.7431 -0.5076 0.2734 0.4250 0.2200 0.7192 0.4551 -0.2876 0.3299 -0.6889 -0.2404 -0.8372 0.0219 -0.0181 0.1463 -0.5245 0.2721 0.1144 0.1633 -0.3012 0.2346 0.4896 0.0539 -0.4367 -0.4442 -0.1432 0.3200 -0.3041 0.4268 -0.1290 -0.3429 0.5477 -0.0069 -0.1900 -0.3022 0.6622 -0.0253 0.3395 0.0301 -0.2680 -0.0379 0.1428 0.3302 0.0596 0.5808 -0.0830 -0.2721 0.6924 0.0888 0.8348 0.1913 0.0173 -0.1094 0.3777 0.2724 -0.4384 -0.0410 0.3503 0.5023 0.2206 -0.5890 0.0367 -0.3338 0.9258 -0.4508 -0.2004 0.0719 1.0938 0.8232 -0.2318 0.1890 0.1043 -0.4000 0.2134 -0.2826 -0.1097 0.6548 0.4961 0.3926 -0.5436 -0.1727 -0.5459 0.1039 0.0243 0.1120 -0.1421 -0.3257 0.8620 -0.3420 0.0868 -0.4102 -0.3604 -0.1350 0.4695 0.0834 0.3250 -0.3574 -0.1595 0.1768 -0.0326 -0.2493 0.1360 0.2644 0.3307 -0.0123 0.0974 0.2792 -0.6300 0.5049 -0.0481 -0.0354 -0.8216 -0.1767 -0.3365 0.1326 0.4728 -0.3519 -0.2839 0.4667 0.1003 0.1995 -0.4195 -0.4603 1.2199 -0.0732 -0.5929 -0.5021 -0.7128 -0.6214 0.3192 0.1684 -0.2102 0.4546 0.3216 0.6002 0.0061 0.2754 0.0905 -0.4532 -0.4504 0.4331 0.2006 -0.0750 -0.7206 -0.8921 0.5873 -0.0347 -0.1797 -1.4079 -0.2568 0.0494 -0.0938 0.9003 0.1116 -0.6840 -0.5167
whos
Name Size Bytes Class Attributes a 1x5 9904840 cell b 3439x72x5 9904320 double bmean 3439x72 1980864 double cmdout 1x33 66 char i 1x1 8 double m 1x1 1980968 cell s 3439x72 1980864 double

6 Comments

Thank you so much very great help.
MP
MP on 9 Oct 2022
Edited: MP on 9 Oct 2022
That appears to create some trouble when NaN values are present. Could you please help.
I want NaNMean.
see the update for handling nan.
Hi Chunru. Thank you very much for helping.
This example works fine here and other cases. But why does it show wrong output for my actual data.
Here is my actual data attached. I hope you will be able to understand it.
here "bmean.mat" does not show the correct output of mean values in matrix "a.mat".
Here is my code:
Am I doing something wrong?
Could you please help.
FODU5 = cell2mat(FODU4);
FODU5 = reshape(FODU5, 3439, 72, []);
FODU6 = mean(FODU5, 3, 'omitnan');
%load(websave("bmean.mat", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1150810/bmean.mat"))
%load(websave("b.mat", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1150815/b.mat"))
load(websave("a.mat", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1150820/a.mat"))
FODU4 % This is a 5x1 cell array instead of 1x5 array
FODU4 = 5×1 cell array
{3439×72 single} {3439×72 single} {3439×72 single} {3439×72 single} {3439×72 single}
FODU5 = cell2mat(FODU4'); % tranpose the cell array so that cell2mat works
FODU5 = reshape(FODU5, 3439, 72, []);
FODU6 = mean(FODU5, 3, 'omitnan');
whos
Name Size Bytes Class Attributes FODU4 5x1 4952680 cell FODU5 3439x72x5 4952160 single FODU6 3439x72 990432 single cmdout 1x33 66 char
Yes, transpose did work!!
Thank you so very much.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

MP
on 9 Oct 2022

Commented:

MP
on 11 Oct 2022

Community Treasure Hunt

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

Start Hunting!