16
>> X = [0 1 2
        3 4 5]

>> sum(X, 1)

ans =

     3     5     7

sum(X, 1) should sum along the 1st dimension(row) as per the document says:

S = SUM(X,DIM) sums along the dimension DIM.

But why does it actually sums along the 2nd dimension(column)?

MatlabDoug
  • 5,704
  • 1
  • 24
  • 36
Gtker
  • 2,237
  • 9
  • 29
  • 37

4 Answers4

27

In my opinion, it is perfectly consistent with everything else.

sum(A,dim) sums along the direction of dimension dim.

Rows are counted "down", so sum(A,1) sums "down". Columns are counted "to the right", so sum(A,2) sums "to the right".

Another way to look at this is that sum(A,dim) collapses dimension dim to 1 by taking the sum. Thus, a 4x3 array summed along dimension 1 collapses the first dimension, leading to a 1x3 array.

Jonas
  • 74,690
  • 10
  • 137
  • 177
  • 9
    I also keep it straight in my head using the "dim is the dimension to squash" approach. Also consider arrays with 3 or more dimensions; for me, it's easier to see why it should be this way when you get away from the 2D case. – Arthur Ward Jun 29 '10 at 14:18
  • 3
    +1 for not just copy-pasting the doc (which Gtker had read but not understood) and instead explaining how to interpret it. – Calimo Oct 23 '13 at 14:06
2

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html

B = sum(A,dim) sums along the dimension of A specified by scalar dim. The dim input is an integer value from 1 to N, where N is the number of dimensions in A. Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.

Your guess is as good as mine.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • But the 1st dimension is row,and 2nd column,why it's not the case for `sum`? – Gtker Apr 16 '10 at 07:27
  • Hence my "Your guess is as good as mine" - there's no real logical reason for it, so it's probably just the result of some random MATLAB developer's late-night coding session. ;) – Amber Apr 16 '10 at 11:08
2

1 means column, according to http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html

B = sum(A,dim) sums along the dimension of A specified by scalar dim. The dim input is an integer value from 1 to N, where N is the number of dimensions in A. Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.

Ledhund
  • 1,228
  • 10
  • 24
  • Why it's contradictory with other context ? – Gtker Apr 16 '10 at 07:29
  • 2
    It might actually be indicating the axis along which the primary iteration is occurring. (Sometimes these things are hard to understand with only low-dimensional examples.) – Donal Fellows Apr 16 '10 at 12:59
0

I think that the Matlab documentation on this is quite clear. It states:

B = sum(A,dim) sums along the dimension of A specified by scalar dim. The dim input is an integer value from 1 to N, where N is the number of dimensions in A. Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.

You're welcome to think that Matlab is wrong, but it ain't going to change !

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • The offline version only mentions:`S = SUM(X,DIM) sums along the dimension DIM. ` and that's all! – Gtker Apr 16 '10 at 07:31