Sum of a cell

  • Follow


http://www.sendspace.com/file/xeorjs
Open that in matlab and it will be stored into the workspace



I'm suppose to add all the elements in y and give the sum.


would it be like sum(y)

but i tried that and it doesnt work ?
0
Reply Judas 4/7/2010 7:03:06 PM

How about just typing in a simple example which represents your problem, rather than asking strangers to trust that you aren't having them download something malicious?

A = {1 2 3 4};  % Does your data look like that?

sum([A{:}])  % If so.
0
Reply Matt 4/7/2010 7:09:10 PM


Sorry it's like this

myCell = cell(1,3)

myCell{1,1} =[1,4,-10;]




myCell{1,2} =[9,5,3,8,9,2,1,0,8,9,3,5,0,7,2,5,1,0,5,9,6,5,1,1,0,2,2,9,4,9,1,7,3,2,0,3,2,7,6,5,5,2,6,9,2,6,8,3,4,6,4,5,6,1,8,4,5,2,5,8,7,3,8,6,1,6,0,3,4,6,7,9,4,6,4,1,8,4,3,5,0,5,9,8,5,3,2,0,0,2,7,3,4,8,1,9,0,2,6,5;]


myCell{1,3} = [25,21,35,29,99,69;1,56,62,54,3,94;92,4,45,83,2,91;61,3,9,65,58,100;59,90,21,28,26,63;82,80,66,18,98,65;65,62,31,44,80,56;44,86,83,24,65,96;]






"Matt Fig" <spamanon@yahoo.com> wrote in message <hpil8l$grh$1@fred.mathworks.com>...
> How about just typing in a simple example which represents your problem, rather than asking strangers to trust that you aren't having them download something malicious?
> 
> A = {1 2 3 4};  % Does your data look like that?
> 
> sum([A{:}])  % If so.
0
Reply Judas 4/7/2010 7:34:07 PM

O.k., so now you need to specify whether or not you need some sort of 'grand total', or just the sum of elements in each cell.  This is because the SUM function behaves differently for different sizes of inputs.

myCell = {magic(4), [1 2 3], [7;8;9], rand(6,7)}  % A simple example.

sum(cellfun(@(x) sum(x(:)),myCell))  % Grand total over all

cellfun(@sum,myCell,'Un',0)  % Simple application of SUM for each cell

cellfun(@(x) sum(x(:)),myCell)  % Grand total over all elements of each cell
0
Reply Matt 4/7/2010 7:43:05 PM

3 Replies
297 Views

(page loaded in 0.284 seconds)


Reply: