Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask MATLAB Expert

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.

MATLAB, Engineering

  • Category:- MATLAB
  • Reference No.:- M9512493

Have any Question?


Related Questions in MATLAB

Suppose that a student has the option of enrolling for a

Suppose that a student has the option of enrolling for a single elective during a term. The student must select a course from a limited list of options: "English, " "History, " "Biology, " "Computer, " or "Math." Constru ...

Assignment - matlab programmingusing appropriate matlab

Assignment - MatLab Programming Using appropriate MatLab syntax, write the code required to analyse and display the data as per the problem description. The order of the MatLab Program should be as follows: Variables and ...

Recitation problems -1 determine the highest real root of

Recitation Problems - 1. Determine the highest real root of f(x) = 2x 3 - 11.7x 2 + 17.7x - 5 using the Newton-Raphson method with at least four iterations. Start with an initial guess of x 0 = 3. 2. Determine the real r ...

Question - verify the attached paper with matlab and get

Question - Verify the attached paper with matlab and get all the results in the paper and explain step by step the matlab code. Paper - Improving Massive MIMO Belief Propagation Detector with Deep Neural Network. Attachm ...

Assignment -we have daily gridded rainfall data of 40 years

Assignment - We have daily gridded rainfall data of 40 years and structure of the dataset is like below; Lat = [6.5:0.25:38.5]; Lon = [66.5:0.25:100]; Rainfall (135x129x365x40) (Lon, Lat, days, years). Now, we looking fo ...

Assignment -matlab codes and simulated model in

Assignment - Matlab codes and simulated model in simulink/matlab and truetime. 1. Matlab codes and simulink model for pid controller optimization using particle swarm optimization (PSO) my plant is integer order 1000/(s^ ...

Assignment - matlab programmingusing appropriate matlab

Assignment - MatLab Programming Using appropriate MatLab syntax, write the code required to analyse and display the data as per the problem description. The order of the MatLab Program should be as follows: Variables and ...

Assignmentafter the success of your robo-advice venture you

Assignment After the success of your robo-advice venture you decide to explore alternative sources of profitability for your company. You realize that Australian investors are often forced to chose between expensive acti ...

What comparison of means test was used to answer the

What comparison of means test was used to answer the question

Question 1 manipulate spectral imagehyperspectral images

Question 1. Manipulate spectral image Hyperspectral images can be seen as a generalisation of normal colour images such as RGB images. In a normal RGB colour image, there are 3 channels, i.e. channels for red colour, gre ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As