Examples on using the sum function in Octave
If you are taking Andrew Ng's Machine Learning course on Coursera, the sum
function is one of the functions that you will probably need to use to complete your assignments. Since the sum
function can be counter intuitive in some cases, I document some of the examples that I had created while trying to get a better understanding of the sum function in Octave.
Using sum on a row vector in Octave
When the sum
function encounters a row vector, it basically sums all the elements in the row vector and returns the total:
>> rowVector = [1 2 3] rowVector = 1 2 3 >> sum(rowVector) ans = 6
Using sum on a column vector in Octave
As with the case of using the sum
function on a row vector, when the sum
function encounters a column vector, it will add all the elements in the column vector and returns the total:
colVector = [1; 2; 3] colVector = 1 2 3 >> sum(colVector) ans = 6
Using sum
on a matrix in Octave
When a matrix is given to sum
as a parameter, the behaviour can be counter intuitive at first. Although you may expect that the sum
function returns a single value that is the total of adding all the elements in the matrix together, this is not the case.
To see how the sum
function handles matrix as an input, let's first define a 2 by 3 matrix:
>> x = [1 2 3; 1 2 3] x = 1 2 3 1 2 3
How to sum a matrix by column
Without any additional parameters to the matrix, the sum function collapses the matrix into a row vector by adding elements in the same column together:
>> sum(x) ans = 2 4 6
This behaviour is similar to supplying a '1' as an additional parameter to the matrix:
>> sum(x, 1) ans = 2 4 6
How to sum a matrix by row
If a '2' is supplied with the matrix to the sum
function, the sum
function will take return a column vector that contains the subtotal of all items in each row added together:
>> sum(x, 2) ans = 6 6
How to sum all elements in the matrix
Summing all the elements in the matrix was what I had required in computing the cost function of my learning algorithm. There are three ways which I had tried:
-
Summing the matrix by column and then summing the resultant row vector:
>> sum(sum(x , 1)) ans = 12
-
Summing the matrix by row and then summing the resultant column vector:
>> sum(sum(x , 2)) ans = 12
-
Converting the matrix to a column vector and then summing the resultant column vector:
>> sum(x(:)) ans = 12