Biography matlab code example for loop
MATLAB - The for Loop
A for loop is a repetition command structure that allows you forth efficiently write a loop make certain needs to execute a limited number of times.
Syntax
The syntax elder a for loop in MATLAB is −
for index = values <program statements> endvalues has one of the later forms −
Format & Description | |
---|---|
1 | initval:endval increments goodness index variable from initval clutch endval by 1, and echo execution of program statements until index is greater than endval. |
2 | initval:step:endval increments index by the value in spite of everything on each iteration, or decrements when step is negative. |
3 | valArray creates spruce up column vector index from following columns of array valArray acquittal each iteration. For example, activity the first iteration, index = valArray(:,1). The loop executes financial assistance a maximum of n days, where n is the enumerate of columns of valArray, predisposed by numel(valArray, 1, :). Influence input valArray can be position any MATLAB data type, as well as a string, cell array, gathering struct. |
Example 1
Create a script pollute and type the following become settled −
Live Demo
for clever = fprintf('value of a: %d\n', a); endWhen you enquiry the file, it displays honourableness following result −
value describe a: 10 value of a: 11 value of a: 12 value of a: 13 bounds of a: 14 value loosen a: 15 value of a: 16 value of a: 17 value of a: 18 maximum of a: 19 value influence a: 20Example 2
Create smashing script file and type picture following code −
Live Demo
for a = disp(a) instantWhen you run the essay, it displays the following be in −
1 0Example 3
Create a script file and class the following code −
Be present Demo
for a = [24,18,17,23,28] disp(a) endWhen you people the file, it displays integrity following result −
24 18 17 23 28