Average each element of cell array

47 views (last 30 days)
Carmel Howe
Carmel Howe on 25 Oct 2015
Commented: Paolo on 14 Jul 2018
I have a 10x1 cell of separate events with each array having a size 384(time) x 5328(pixel). I want to average each pixel across each separate event. For example A{1,1}(1,1); averaged with A{2,1}(1,1) etc etc Thanks

Accepted Answer

Stephen23
Stephen23 on 25 Oct 2015
Edited: Stephen23 on 25 Oct 2015
The easiest way is to use cat to concatenate the cell array of numeric arrays into a simple 3D numeric array, and then use the inbuilt mean function with its optional second argument to take the average along the third dimension:
X = your cell array
Y = cat(3,X{:});
out = mean(Y,3);
Here is a simple example showing how this works:
>> X = {[1,2,3;4,5,6],[0,2,4;6,8,10]};
>> Y = cat(3,X{:})
Y(:,:,1) =
1 2 3
4 5 6
Y(:,:,2) =
0 2 4
6 8 10
>> out = mean(Y,3)
out =
0.5 2 3.5
5 6.5 8
  3 Comments
Chalita
Chalita on 14 Jul 2018
Edited: Chalita on 14 Jul 2018
Hi. I'm new to matlab. This answer is very helpful for what I'm trying to do. Thanks.
I have about 1,000 cells in my cell array. What do I need to do if instead of taking average across all cells, I want to take average every 4 cells. That is, instead of 1 output cell, I should have 1,000/4 cells. Thanks
Paolo
Paolo on 14 Jul 2018
@Chalita you should open a new question for your query.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!