Mean values of cell arrays

82 visualizaciones (últimos 30 días)
Luigi
Luigi el 7 de Dic. de 2016
Comentada: Luigi el 12 de Dic. de 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

Respuesta aceptada

Star Strider
Star Strider el 7 de Dic. de 2016
Editada: Star Strider el 7 de Dic. de 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 comentarios
Luigi
Luigi el 7 de Dic. de 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 el 7 de Dic. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

Guillaume
Guillaume el 7 de Dic. de 2016
Editada: Guillaume el 7 de Dic. de 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 comentario
Luigi
Luigi el 12 de Dic. de 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

Iniciar sesión para comentar.


Adam
Adam el 7 de Dic. de 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 comentarios
Luigi
Luigi el 7 de Dic. de 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 el 7 de Dic. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by