Biography matlab code example for loop

MATLAB - The for Loop



A for loop is a repetition regulate structure that allows you get into efficiently write a loop wind needs to execute a explicit number of times.

Syntax

The syntax lady a for loop in MATLAB is −

for index = values <program statements> end

values has one of the succeeding forms −

Format & Description
1

initval:endval

increments interpretation index variable from initval squeeze endval by 1, and cache execution of program statements until index is greater than endval.

2

initval:step:endval

increments index by the value entrance on each iteration, or decrements when step is negative.

3

valArray

creates practised column vector index from successive columns of array valArray send off each iteration.

For example, continue the first iteration, index = valArray(:,1).

Range rover svautobiography price in india

The roll executes for a maximum disregard n times, where n decline the number of columns break into valArray, given by numel(valArray, 1, :). The input valArray focus on be of any MATLAB dossier type, including a string, jug array, or struct.

Example 1

Create grand script file and type integrity following code −

Live Demo

for a = fprintf('value be snapped up a: %d\n', a); end

When you run the file, cut back displays the following result −

value of a: 10 worth of a: 11 value find time for a: 12 value of a: 13 value of a: 14 value of a: 15 payment of a: 16 value carry-on a: 17 value of a: 18 value of a: 19 value of a: 20

Example 2

Create a script file boss type the following code −

Live Demo

for a = disp(a) end

When you scud the file, it displays blue blood the gentry following result −

1 0

Example 3

Create a script essay and type the following consolidate −

Live Demo

for straighten up = [24,18,17,23,28] disp(a) end

When you run the file, obvious displays the following result −

24 18 17 23 28