MATLAB Answers

Find sum of elements in a cell along the columns

3 views (last 30 days)
Hi all,
I have a 3D cell A with dimensions 3x5x2. An example of elements along row 2, all columns and z dimension = 1 is:
x = A(2,:,1)
x = [ ] [ ] 5.1 [ ] 7.9
I would like to add up the non zero entries and get the number of non zero entries in this resulting array. I tried using:
y = sum([A(2,:,1)])
y = cumsum([A(2,:,1)])
But got the following error:
"Error using sum
Invalid data type. First argument must be numeric or logical."
Could anyone please help me with this and would kindly let me know the procedure to solve such problems.

  1 Comment

Fabio Freschi
Fabio Freschi on 9 Sep 2019
Could you please load your data in a mat file?

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 9 Sep 2019
Edited: James Tursa on 9 Sep 2019
Is this what you are trying to do? (using the curly braces)
y = sum([A{2,:,1}])

  1 Comment

Vishakha Ramani
Vishakha Ramani on 9 Sep 2019
Thank you. Can't believe this was that simple. Also, could you please tell me how would you find the number of non-zero entries in such a case?

Sign in to comment.

More Answers (1)

Jacob Shulman
Jacob Shulman on 9 Sep 2019
for i=1:rows
for j=1:columns
if nonzeros(A{i,j,1})
y = sum([A{2,:,1}])
The function nonzeros will not count zeros or blanks.


Sign in to comment.


Translated by