1

I want to sum elements of array which every element is a matrix.

I wrote below but not working:

AA={[1 2;3 4],[5 6;7 8]}
i=1:2;
sum(AA{i})
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Masoud
  • 27
  • 3

1 Answers1

3

If you are wanting to perform operations across a set of 2-D matrices, all of which are the same size (and not too huge), it's easiest to store them as a 3-D matrix instead. See here for discussion/examples.

If you already have your matrices in a cell array, as in your example, you can concatenate them into a 3-D matrix using cat and sum across the third dimension without a for loop using sum:

mat = sum(cat(3, AA{:}), 3);
Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359