sum of all numbers in the third column in matlab

Assuming that you have a matrix A and the third column contains the numbers you want to sum, you can use the following code to sum all the numbers in the third column:

main.m
sum = 0;
for i = 1:size(A,1)
    sum = sum + A(i,3);
end
57 chars
5 lines

Here, we initialize a variable sum to 0, and then loop through every row in matrix A using a for loop. In each iteration of the loop, we add the value in the third column of the current row to sum. Finally, after all the rows have been processed, sum will contain the sum of all the numbers in the third column of the matrix.

gistlibby LogSnag