Mean values of cell arrays

87 views (last 30 days)
Luigi
Luigi on 7 Dec 2016
Commented: Luigi on 12 Dec 2016
Hello to everybody, I have a problem. I have a cell array of dimension nxm, each element of the cell is a vector of 1xp, so i have m 1xp vectors for each of n rows of the cell. I want to create an array of nxp containing the mean value of each element (with index p) across all the cell element (index m), so the resulting array will have as first element the mean of the first element of all the cell elements and so on. Is there a function to do that? Can someone help me (hopefully showing me the code or the pseudo-code)? Thank you so much. Best regards, Luigi

Accepted Answer

Star Strider
Star Strider on 7 Dec 2016
Edited: Star Strider on 7 Dec 2016
I am not certain that I understand what you want.
If ‘C’ is your cell array, I would do something like this:
M = cell2mat(C);
Result = [mean(M,2) M];
EDIT Changed to include new data (16:32 UTC):
C = {2,6,7; 3,13,68;54, 3, 76};
result = mean(cell2mat(C))
result =
19.667 7.3333 50.333
  2 Comments
Luigi
Luigi on 7 Dec 2016
Thank you so much, I've tried with your code, it seems that we are close to the solution, but still is not exactly what i want, because the output array have a dimension of n x q
where q = m x p
I want do the following:
Let’say that the my cell is C = {2,6,7; 3,13,68;54, 3, 76};
I want to have as result this array:
result = [mean(2,13,54), mean(6,13,3), mean(7,68,76)]
Star Strider
Star Strider on 7 Dec 2016
My pleasure.
This seems to be an error:
mean(2,13,54)
since I believe it should be:
mean(2,3,54)
to be consistent with the other values.
I am obviously not understanding something, because my code creates the result you want:
result = [mean([2,3,54]), mean([6,13 3]), mean([7,68,76])]
produces:
result =
19.667 7.3333 50.333
as in my original Answer.

Sign in to comment.

More Answers (2)

Guillaume
Guillaume on 7 Dec 2016
Edited: Guillaume on 7 Dec 2016
As others have said, using a cell array for storing matrices all the same size is a waste as it makes manipulation harder. So, the first thing to do is to convert your 2d cell array of 1d vectors into a 3d matrix. It's up to you how you map the cell array / vector dimensions to the matrix dimensions. One option:
n = 5; m = 3; p = 7; %demo constants
c = squeeze(num2cell(reshape(1:n*p*m, [n p m]), 2)) %demo cell array creation, a n*m cell array of 1*p vectors
casmatrix = permute(reshape([c{:}], [], size(c, 1), size(c, 2)), [2 3 1])
this creates an n*m*p matrix, but you could have a n*p*m, p*n*m, n*p*m matrix, whichever you prefer for visualisation, by just changing the order of dimensions in permute.
Calculating your mean along the m dimension is then trivial. In the above example:
squeeze(mean(casmatrix, 2))
is all that is needed to get your n*p mean.
  1 Comment
Luigi
Luigi on 12 Dec 2016
Good morning, I'm sorry for late reply. I would like to thank you for your suggestions and for your help. It works. Thank you very much. Luigi

Sign in to comment.


Adam
Adam on 7 Dec 2016
You should be able to use cell2mat to convert to a normal numeric array if your cell contents are always all the same size as each other. Ideally you should use a numeric array in the first place for this and never need to be in a cell array.
Once you have converted to a numeric array it is just a simple usage of the mean function along the right dimension.
  2 Comments
Luigi
Luigi on 7 Dec 2016
Thank you so much, but i don't think i have understood what you mean.
I want do the following:
Let’say that the my cell is C = {2,6,7; 3,13,68;54, 3, 76};
I want to have as result this array:
result = [mean(2,13,54), mean(6,13,3), mean(7,68,76)]
Adam
Adam on 7 Dec 2016
Your original question said each element of your cell array is a vector, here each element is a scalar and there is no value whatsoever in having a cell array.
Just a basic
C = cell2mat(C)
will give you a numeric array and
mean( C )
gives the result you ask for so
mean( cell2mat( C ) )
works in this small example.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!