# sum Things to Consider

I was just helping someone debug a piece of code that was giving an incorrect answer. The code returned an output with a shape different than the coder expected. I made sure the workspace browser was showing while we worked, and as we stepped through the algorithm, we were able to see exactly where the problem occurred. And it was the call to sum.

### By Default, Column-wise Operations

By default, MATLAB performs many operations by treating the columns as individual vectors and acting on them. However, if the array has only a single element per column, then MATLAB performs the operation along the first non-singleton dimension.

### User Issue and Solution

So, that's the default behavior. However, for this user's application, he always wanted the sum to be along the first dimension (down the columns), even if there was only a single entry per column.

To accomplish this, the altered code used the optional second input argument, dimension. This works similarly with other functions that typically reduce the dimensionality from the input to the output, such as

Here's the relevant portion of the help for sum. I happen to know that I only need to go up through the second instance of DIM.

h = help('sum');
f = strfind(h,'DIM');
disp(h(1:f(2)+5))
 SUM Sum of elements.
S = SUM(X) is the sum of the elements of the vector X. If
X is a matrix, S is a row vector with the sum over each
column. For N-D arrays, SUM(X) operates along the first
non-singleton dimension.
If X is floating point, that is double or single, S is
accumulated natively, that is in the same class as X,
and S has the same class as X. If X is not floating point,
S is accumulated in double and S has class double.

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



### Examples

Let's see this behavior in action.

n = 4;
A = cat(3, pascal(4), magic(4), invhilb(4), hadamard(4), hilb(4))
A(:,:,1) =
1     1     1     1
1     2     3     4
1     3     6    10
1     4    10    20
A(:,:,2) =
16     2     3    13
5    11    10     8
9     7     6    12
4    14    15     1
A(:,:,3) =
16        -120         240        -140
-120        1200       -2700        1680
240       -2700        6480       -4200
-140        1680       -4200        2800
A(:,:,4) =
1     1     1     1
1    -1     1    -1
1     1    -1    -1
1    -1    -1     1
A(:,:,5) =
1.0000    0.5000    0.3333    0.2500
0.5000    0.3333    0.2500    0.2000
0.3333    0.2500    0.2000    0.1667
0.2500    0.2000    0.1667    0.1429


Sum A down the columns.

sumA = sum(A)
sumA(:,:,1) =
4    10    20    35
sumA(:,:,2) =
34    34    34    34
sumA(:,:,3) =
-4    60  -180   140
sumA(:,:,4) =
4     0     0     0
sumA(:,:,5) =
2.0833    1.2833    0.9500    0.7595


Sum A along the rows.

sumA2 = sum(A,2)
sumA2(:,:,1) =
4
10
20
35
sumA2(:,:,2) =
34
34
34
34
sumA2(:,:,3) =
-4
60
-180
140
sumA2(:,:,4) =
4
0
0
0
sumA2(:,:,5) =
2.0833
1.2833
0.9500
0.7595


Sum A across the third dimension.

sumA3 = sum(A,3)
sumA3 =
1.0e+003 *
0.0350   -0.1155    0.2453   -0.1248
-0.1125    1.2123   -2.6858    1.6912
0.2513   -2.6888    6.4912   -4.1788
-0.1338    1.6972   -4.1758    2.8221


Try summing A along the 7th dimension.

sumA7 = sum(A,7)
sumA7(:,:,1) =
1     1     1     1
1     2     3     4
1     3     6    10
1     4    10    20
sumA7(:,:,2) =
16     2     3    13
5    11    10     8
9     7     6    12
4    14    15     1
sumA7(:,:,3) =
16        -120         240        -140
-120        1200       -2700        1680
240       -2700        6480       -4200
-140        1680       -4200        2800
sumA7(:,:,4) =
1     1     1     1
1    -1     1    -1
1     1    -1    -1
1    -1    -1     1
sumA7(:,:,5) =
1.0000    0.5000    0.3333    0.2500
0.5000    0.3333    0.2500    0.2000
0.3333    0.2500    0.2000    0.1667
0.2500    0.2000    0.1667    0.1429


### How Can That Be?

Why did I get no error for summing along dimension 7 when I only have a 3 dimensional array? The reason is that MATLAB treats all arrays as having an infinite number of dimensions, most of them trailing singletons. So if I sum along the 7th dimension here, I am only summing single elements, or effectively in this case just returning the original array.

### Ever Used Dimension to Your Advantage?

Have you ever used the dimension argument to your advantage? Or tripped over the issue of having an unexpected singleton dimension? Any thoughts, please post them here.

Published with MATLAB® 7.4

|

### Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.