# Sum of cell array Line

3 views (last 30 days)
Marco Silva on 7 Aug 2019
Commented: Adam Danz on 8 Aug 2019
Hi,
I have a cell array and i need to get the sum of line 4. My cell array is:
A(4,:) =
1×25 cell array
Columns 1 through 8
{0×0 double} {1×45 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
Columns 9 through 16
{0×0 double} {0×0 double} {0×0 double} {1×45 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
Columns 17 through 24
{0×0 double} {1×45 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {1×45 double} {0×0 double}
Column 25
{0×0 double}
The problem is, I need my result in format 1x45 double. I need to add each element individually.
Like this,
If A(4,2) = { 1 2 3 4 5} and A(4, 12) = { 6 7 8 9 10} my result should be: Result = { 3.5 4.5 5.5 6.5 7.5}
how can i do this?
Thanks

Adam Danz on 7 Aug 2019
Edited: Adam Danz on 8 Aug 2019
% When elements of A are row vectors of the same length
% (like the example in the question)
s = sum(cell2mat(A{4}.'),1);
% When elements of A are column vectors of the same length
s = sum(cell2mat(A{4}),2);

Marco Silva on 8 Aug 2019
Thanks for the help, it works!
Adam Danz on 8 Aug 2019