Illustration of nested loops, MATLAB in Statistics

Assignment Help:

Illustration of Nested loops:

Running the script shows the output:

>> printstars

*****

*****

*****

The variable rows identifies the number of rows to print, and the variable columns identifies how many *'s to print in each row. There are 2 loop variables: i is the loop variable for the rows and j is the loop variable for columns. As the number of rows and columns are well-known, the for loops are used. There is one for loop to loop over the rows, and the other to print the needed number of *'s. The values of the loop variables are not used within the loops, but are used easily to iterate the right number of times. The first for loop identifies that the action will be repeated rows times. The action of this loop is to print *'s and then the newline character. Particularly, the action is to loop to print the columns *'s across on one line. The newline character is then printed after all five stars to move the cursor down for the next line.

The first for loop is known as the outer loop; the second for loop is known as the inner loop. Therefore, the outer loop is over the rows, and the inner loop over the columns. The outer loop should be over the rows as the program is printing a certain number of rows of output. For each row, a loop is essential to print the needed number of *'s; this is the inner for loop.

When this script is executed, initially the outer loop variable i is initialized to 1.And then action is executed. The action contains of the inner loop, and printing the newline character. Therefore, while the outer loop variable has value 1, the inner loop variable j iterates through its all values. As the value of columns is 5, the inner loop will print a * 5 times. Then, the newline character is printed and the outer loop variable i is incremented to 2. The action of outer loop is then executed once again, means that the inner loop will print five *'s, and then the newline character will be printed. This persists, and in all, the action of the outer loop will be executed rows times.

Note that the action of the outer loop consists of the two statements (that is the for loop and an fprintf statement). The action of the inner loop, though, is only a single statement.

The fprintf statement to print the newline character should be separate from the other fprintf statement which prints the *. If we simply had

fprintf('*\n')

As the action of inner loop, this would print a long column of 15 *'s, but not a box.


Related Discussions:- Illustration of nested loops

Input in a while loop, Input in a While Loop: The script below repeats...

Input in a While Loop: The script below repeats the procedure of prompting a user, the reading in a positive number, and echo-printing it, as long as the user properly enters

Assignment , Determine sequence weights for the sequences ACTA, ACTT, CGTT,...

Determine sequence weights for the sequences ACTA, ACTT, CGTT, and AGAT in problem 1 by using Thompson, Higgins, and Gibson method a) compute pairwise distances between sequences

Illustration of customizing plots, Illustration of Customizing plots: ...

Illustration of Customizing plots: Illustration, the bar and barh functions by the default place a width of 0.8 between bars. Whenever called as bar(x,y), the width of 0.8 is

Illustration of indexing into vectors of structures, Illustration of Indexi...

Illustration of Indexing into vectors of structures: For illustration, the function to print the information from packages has been modified therefore in addition to the vecto

Menu function, MENU Function: The MATLAB also has a built-in function ...

MENU Function: The MATLAB also has a built-in function known as menu which will display a figure Window with push buttons for the choices. The very first string passed to the

Function polyval - interpolation, Function polyval - interpolation: Th...

Function polyval - interpolation: The function polyval can then be used to compute the polynomial at particular values. For illustration, we could compute at every value in th

Example of core objects, Example of Core objects: The one core graphic...

Example of Core objects: The one core graphics object is a line that is also what the plot function produces. Here is an illustration of generating a line object, altering som

Data structure layers, Data structure layers: There are many layers in...

Data structure layers: There are many layers in this variable. For illustration,  cyls is the whole data structure, that is a vector of structs  cyls(1) is a separate

Core objects, Core Objects: The Core Objects in MATLAB are the very fu...

Core Objects: The Core Objects in MATLAB are the very fundamental graphics primitives. The description can be found under the MATLAB Help: Under the Contents tab, click the Ha

Example of function stubs, Example of Function stubs: The three functi...

Example of Function stubs: The three functions have not so far been written, though, therefore the function stubs are put in place so that the script can be executed and teste

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd