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)?