For loop, MATLAB in Engineering

FOR Loop:

The for loop, or the for statement, is used whenever it is essential to repeat statement(s) in the script or function, and whenever it is known ahead of time how many times the statements will be repeated. The statements which are repeated are termed as the action of the loop. For illustration, it might be known that the action of the loop will be repeated five times. The terms used are that we iterate through the action of the loop five times.

The variable which is used to iterate through values is termed as the loop variable, or an iterator variable. For illustration, the variable may iterate through the integers 1 through 5 (example, 1, 2, 3, 4, and then 5). Though variable names in common must be mnemonic, it is general for an iterator variable to be given the name i (and if more than one iterator variable is required, i, j, k, l, etc.) This is historical, and is since of the way integer variables were named in the FORTRAN.

Though, in MATLAB both i & j are built-in values for  1 - , therefore using either as a loop variable will override that value. If this is not an issue, then it is acceptable to use i as a loop variable.

The common form of the for loop is as shown below:

for loopvar = range

action

end

where loopvar is the loop variable, range is the range of values by which the loop variable is to iterate, and the action of loop contains all  the statements up to the end. The range can be identified by using any vector, but usually the simplest way to identify the range of the values is to use the colon operator.

Posted Date: 10/19/2012 3:30:03 AM | Location : United States







Related Discussions:- For loop, Assignment Help, Ask Question on For loop, Get Answer, Expert's Help, For loop Discussions

Write discussion on For loop
Your posts are moderated
Related Questions
Illustration of gauss-jordan: Here's an illustration of performing such substitutions by using MATLAB >> a = [1 3 0; 2 1 3; 4 2 3] a = 1 3 0 2 1 3 4 2

Matrix definitions: As we know the matrix can be thought of as a table of values in which there are both rows and columns. The most common form of a matrix A (that is sometime

Finding sums and products: A very general application of a for loop is to compute sums and products. For illustration, rather than of just printing the integers 1 through 5, w

Technique to create Nested structures: This technique is the most proficient. Though, the other technique is to build the nested structure one field at a time. As this is a ne

Example to change the line width from the default: For illustration, to change the line width from the default of 0.5 to 1.5: >> set(hl,'LineWidth',1.5) As long as the

Algorithm for subfunction: The algorithm for subfunction askforn is as shown:  Prompt the user for the positive integer n.  Loop to print an error message and reprom

Use of While loop: Here is an illustration of calling the function, passing 5000 for the value of the input argument high. >> factgthigh(5000) ans = 5040 The itera

Splits a string : The strtok function splits a string into pieces; it can be called in many ways. The function receives one string as an input argument. It appears for the fir

Program to Counting in a while loop: The script initializes variable counter to 0. Then, in the while loop action, each and every time the user successfully enter a number, th

Expanding a function: The expand function will multiply out terms, and factor will do the opposite: >> expand((x+2)*(x-1)) ans = x^2 x-2 >> factor(ans)