Adding cell array entries

Suppose you have a 162*24 cell array called CC and each cell array entry consist of a matrix.
How would you add CC{1,1}+CC{2,1}+CC {3,1}.....CC{162,1}
and then to the same for the remaining 23 columns.
I dont need the sum but all matrices within each cell entry have to be added.
Thanks

 Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Ag. de 2020

2 votos

nd = ndims(CC{1,1});
sol = sum(cat(ndims+1, CC{:,1}),ndims+1);
There are also approaches using the more obscure fold() operation.

4 comentarios

AA
AA el 24 de Ag. de 2020
Thanks but I dont need the sum. All I need is that the matrices in each cell entry to be added. All matrices have the same dimensions.
madhan ravi
madhan ravi el 24 de Ag. de 2020
This answer does what you asked for.
AA
AA el 24 de Ag. de 2020
I get an error Not enough input arguments and and also an unspecific error in the second line. Maybe the nd variable is missing in the second line?
madhan ravi
madhan ravi el 24 de Ag. de 2020
nd = ndims(CC{1,1});
sol = sum(cat(nd+1, CC{:,1}),nd+1);

Iniciar sesión para comentar.

Más respuestas (1)

Sara Boznik
Sara Boznik el 23 de Ag. de 2020

0 votos

for i=1:24
sol=sum((CC(:,i)))
end

1 comentario

Walter Roberson
Walter Roberson el 23 de Ag. de 2020
No, not for a cell array. sum() does not apply to cell arrays.
Also, you are overwriting all of sol each iteration.

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

AA
el 23 de Ag. de 2020

Comentada:

el 24 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by