In general, to sum up a value in a loop
theSum = 0;
for k = 1 : whatever
theSum = theSum + theVectorValues(k);
I don't know why you can't just use sum(), but just adapt this into your code. Be careful not to call your sum "sum" or you'll blow away the built-in sum() function.