a function loop with one input and one output in matlab

To create a function with loop in Matlab that takes one input and produces one output, use the following syntax:

main.m
function output = functionName(input)
% initialization
output = 0;
% loop
for i = 1:length(input)
    output = output + input(i);
end
end
138 chars
9 lines

This example function takes an input as a vector, initializes the output to zero, and then loops over the input vector elements, adding each element to the output. The resulting output is returned by the function. This is just an example, and you can replace the loop with any other algorithm depending on your task.

gistlibby LogSnag