Friday, September 29, 2017

For Loop Matlab Alongside Examples

For Loop Matlab

For loop is an execution of given dominance repeats itself for a specified break of time. The syntax for For Loop Matlab is
for variable = expression
Program Statement
end

In the higher upward holler syntax, the facial expression has 1 of the next forms.

Initial value : Final value

In the higher upward syntax, initial value increment yesteryear 1 to the finally value. Here is colon operator has used which increment yesteryear default 1. Program repeat the execution of contention until the variable is greater than finally value. The contention may last 1 or to a greater extent than than one.

E.g. 1:10 this volition impress similar 1 ii iii iv five half dozen vii 8 ix 10.

Initial value : Step : Final value

As nosotros know that Matlab for loop increment yesteryear default 1 simply nosotros desire to modify increment value from 1 to ii as well as thence nosotros volition define a stride inwards colon operator.
E.g. 1:2:6 this volition impress similar 1 iii five vii 9.
Now hither inwards higher upward example, you lot run across that the value increment yesteryear ii which is yesteryear default 1.
For decrement role negative break like
10:-2:1 this volition impress similar 10 8 half dozen iv 2.

Value Array

Creates a column vector index from subsequent columns of array valArray on each iteration. For example, on the outset iteration, index = valArray(:,1). The loop executes for a maximum of n times, where n is the break of columns of valArray, given yesteryear numel(valArray, 1, :). The input val Array tin forcefulness out last of whatever MATLAB information type, including a string, jail cellular telephone array, or struct.

Program Example

Here are approximately instance listing below for "for loop" inwards matlab domain.

Examples 1

for x = 1:10
fprintf('value of x: %d\n', x);
halt

Output:

1
2
3
4
5
6
7
8
9
10
Example 2

for x = 1:2:10
fprintf('value of x: %d\n', x);
end

Output:

1
3
5
7
9
Examples 3

for x = 10:-2:0
fprintf('value of x: %d\n', x);
end

Output:

10
8
6
4
2
0
Examples 4

for x = [1 iv half dozen 8 90]
disp(x)
end

Output:

1
4
6
8
90
Watch the next video to sympathise it more.


Reference:                 Mathworks.com