Average each element of cell array

9 Ansichten (letzte 30 Tage)
Carmel Howe
Carmel Howe am 25 Okt. 2015
Kommentiert: Paolo am 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

Akzeptierte Antwort

Stephen23
Stephen23 am 25 Okt. 2015
Bearbeitet: Stephen23 am 25 Okt. 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 Kommentare
Chalita
Chalita am 14 Jul. 2018
Bearbeitet: Chalita am 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 am 14 Jul. 2018
@Chalita you should open a new question for your query.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by