sum numbers from 1 to n in matlab

Here's one way to sum numbers from 1 to n using a loop in MATLAB:

main.m
function result = sumToN(n)
  result = 0;
  for i = 1:n
    result = result + i;
  end
end
91 chars
7 lines

This function takes a single argument n and returns the sum of numbers from 1 to n. It initializes the result to 0, then loops through the numbers from 1 to n and adds each to the result. Finally, it returns the result.

gistlibby LogSnag