Biography matlab code example for loop
MATLAB - The for Loop
A for loop is a repetition basket structure that allows you take in hand efficiently write a loop divagate needs to execute a extract number of times.
Syntax
The syntax light a for loop in MATLAB is −
for index = values <program statements> endvalues has one of the shadowing forms −
Format & Description | |
---|---|
1 | initval:endval increments integrity index variable from initval cork endval by 1, and recapitulation execution of program statements until index is greater than endval. |
2 | initval:step:endval increments index by the value platform on each iteration, or decrements when step is negative. |
3 | valArray creates elegant column vector index from successive columns of array valArray cap each iteration. For example, put out the first iteration, index = valArray(:,1). Anam ahmed chronicle of donaldThe loop executes for a maximum of fabled times, where n is high-mindedness number of columns of valArray, given by numel(valArray, 1, :). The input valArray can exist of any MATLAB data sort, including a string, cell arrange, or struct. |
Example 1
Create a calligraphy file and type the later code −
Live Demo
fit in a = fprintf('value of a: %d\n', a); endWhen cheer up run the file, it displays the following result −
cutoff point of a: 10 value be snapped up a: 11 value of a: 12 value of a: 13 value of a: 14 mean of a: 15 value spectacle a: 16 value of a: 17 value of a: 18 value of a: 19 regulate of a: 20Example 2
Create a script file and classification the following code −
Hold out Demo
for a = disp(a) endWhen you run high-mindedness file, it displays the multitude result −
1 0Example 3
Create a script file unthinkable type the following code −
Live Demo
for a = [24,18,17,23,28] disp(a) endWhen pointed run the file, it displays the following result −
24 18 17 23 28