MATLAB: Mean value in a cell array

cell arraycellfunMATLABmean value

Hi!
I have a 16×8 cell (containing 6×11 cells) with the name FinalResult. I now would like to calculate the mean value of FinalResult{1:16,4}(1,2) and have tried it with
cellfun(@mean,FinalResult{1:16,4}(1,2))
but this will give me the error message "Bad cell reference operation". Where is the mistake?
Thank you in advance for any help.

Best Answer

I'm assuming that the cell array contains matrices not more cell arrays, that is the 6x11 are actually matrices. In which case, this would work:
mean(cellfun(@(c) c(1,2), FinalResult(:, 4)));
What it does is:
  1. Create a cell array that is a subset of your whole array with FinalResult(:, 4). Note that I use () instead of {}. The former returns a cell array, the latter a list of elements.
  2. Iterate over that subset and extract element (1, 2) of each matrix, with cellfun.
  3. get the mean of the elements returned by cellfun