3

Looking at this definition:

: sum (x, dim)

If dim is omitted, it defaults to the first non-singleton dimension.

I tried few commands:

>> sum([2,3,4])         % A matrix of size 1*3 
ans =  9                 
>> sum([2;3;4;])        % A matrix of size 3*1
ans =  9
>> sum([2,3,4;2,3,4;2,3,4;])  % A matrix of size 3*3
ans =
   6   9   12

While these results intuitively don't surprise me much, the result#3 seems to me going against this accepted answer about the definition of "first non-singleton dimension".

Just to assure that it is picking default dim as 1:

>> sum([2,3,4;2,3,4;2,3,4;], 1)         % does COLUMN-WISE SUMMATION
ans =

    6    9   12

>> sum([2,3,4;2,3,4;2,3,4;], 2)         % does ROW-WISE SUMMATION
ans =

   9
   9
   9

So the question is if the accepted answer is correct, shouldn't the summation happen row-wise by default (considering the matrix is 3*3, and the row dimension > 1)?

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
  • 1
    The first dimension that is not a singleton (i.e. size==1) – Ander Biguri Sep 12 '18 at 10:22
  • 4
    The first non-singleton dimension of case #3 is 1, because `size(A,1)=3`, and 3 is greater than 1. Dimension 1 is the columns, because [Matlab is column major](https://stackoverflow.com/questions/21413164/is-matlab-row-specific-or-column-specific). As shown in the [sum documentation](https://uk.mathworks.com/help/matlab/ref/sum.html#btv6ok6-1-dim), when `dim=1`, `sum` operates column-wise. – Wolfie Sep 12 '18 at 10:23
  • @Wolfie. Thanks that makes sense now. – Saurav Sahu Sep 12 '18 at 10:25
  • 1
    Not directly related to your question but to understand a bit more how matlab work, you can also try to run `sum([2,3,4;2,3,4;2,3,4;],3)`, it work because even if you have a 2D matrix, all matrix have an infinite number of trailing singleton dimension. So in this case [3x3x1x1x1x1x....]. – obchardon Sep 12 '18 at 10:34
  • @obchardon Yes I had tried with arbitrary value of `dim` and didn't know why it behaved as if there is a default `dim` value. That too is clear now. thanks. – Saurav Sahu Sep 12 '18 at 10:37
  • The reason `sum(A,35)` works is that a matrix has infinite dimensions. Its just they are singleton, i.e. sized 1 – Ander Biguri Sep 12 '18 at 11:31

1 Answers1

2

The matrices in Matlab have as dimensions (1=rows, 2=columns, 3=depth, ...). Hence, for a matrix

>> A = [2, 3, 4;
        2, 3, 4;
        2, 3, 4]

the summation along dimension 1 will be the summation of the row elements (going down). The summation along dimension 2 will be along the columns (going right), etc.

Now, it is also possible to define a matrix, that does not have rows, but has only columns and depth. Then, because the rows dimension will be zero the command sum(A) will sum along the columns.

>> A = zeros(0,3,3);
>> A(1,:,:) = [2, 3, 4; 2 3 4; 2 3 4]
>> sum(A)
ans(:,:,1) =
     6
ans(:,:,2) =
     9
ans(:,:,3) =
    12
AndP
  • 21
  • 3
  • 1
    Good answer, except you say “the rows dimension will be zero” where you mean “one” — i.e. a singleton dimension. – Cris Luengo Sep 12 '18 at 13:41
  • +1. I always find a good way to think about selecting a 'dimension' in such 'aggregator' functions, is to think of it as choosing the dimension you wish to 'squash' into the aggregated result (i.e. this dimension will be squashed into a size of 1 at the end of this operation). – Tasos Papastylianou Sep 13 '18 at 07:53