for loop in matlab in matlab

The syntax of for loop in MATLAB is:

main.m
for loop_variable = range
    % loop body
end
46 chars
4 lines

Here, loop_variable is the loop counter that takes value from the range in each iteration of the loop. The range can be defined using start:step:end syntax. Using start and step are optional. If step is not defined, it assumes the default value 1.

Here is an example for loop that prints the numbers from 1 to 10 in MATLAB:

main.m
for i = 1:10
    disp(i)
end
29 chars
4 lines

This will produce the following output:

main.m
1
2
3
4
5
6
7
8
9
10
21 chars
11 lines

You can use the loop variable i inside the loop to perform any operation using the current value of i.

related categories

gistlibby LogSnag