Summing matrices in cell array

I have a cell array A that is 9x1 with 9 cells that are all 1x38. I am trying to sum A (sum elements of the 9 cells). I have tried cell2mat,cellfun, sum(cat), and sum, but I keep getting errors with those. Any help is appreciated! Thanks!!

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 10 Jun 2016
Edited: Azzi Abdelmalek on 10 Jun 2016
A={[1 2 3];[4 5 6];[7 8 9]}
out=cellfun(@sum,A)
If A is like this
A={{1 2 3};{4 5 6};{7 8 9}}
out=cellfun(@(x) sum(x{1}),A)

1 Comment

What is x? Here's what I've tried so far: inside A are the 9 cells that are 1x38.
for i=1:38
A=[rnLadp;rnLfie;rnLhaa;rnLhfe;rnLhie;rnLkfe;rnLpap;rnLpie;rnLpud];
C=cell2mat({sum(cat(2,A{:,i}),2)}
end
With this code, I am getting C 38 times but I want to make a cell or matrix or array that has all 38 values from the loop. Advice??

Sign in to comment.

Products

Asked:

on 10 Jun 2016

Edited:

on 10 Jun 2016

Community Treasure Hunt

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

Start Hunting!