Using MATLAB
AB functions st of for, while Similar to other languages, in MATLAB you are able to create a loop to repeat the execution of a group of statements. for and while loop are the two loops. for for loop to repeat specified number of times Syntax for index values end statements Description for index = values, statements, end executes agroup ofstatements in a loop for a specified number of times. values has one of the follo wing forms: initVal:endVa1 Increment the index variable from initval to endVal by 1, and repeat execution of statements until index is greater than endVal. e initVal:stependVal Increment index by the value stepon each iteration, or decrements index when step is negative. evalArray- Create a column vector, index, from subsequent co lumns of array valArray on each iteration. For example, on the first iteration index -valArray(:,1). The loop executes a maximum of n times, where n is the number of columns of valArray, given by numel (valArray (1,:)). The input valArraycan be of any MATLAB data type, including a character vector, cell array, or struct. Example Step by increments of -0.2, and display the values for v 1.0:-0.2:0.0 disp(v) end 0. 8000 0.6000 0.4000 0.2000 while while loop to repeat when condition is true Syntax while expression end Description while expression, statements, end evaluates an cxpression, and repeats the execution of a group of statements in a loop while the expression is true. An expression is true when its result is nonempty and contains only nonzero elements logical or real numerie Otherwise, the expression is false Example: Repeat Statements Until Expression Is False Use a while loop to calculate factorial (10) n-10: while n >1 end disp(l'n!un2str (E)1) The main difference between for and while is that you specify the number of loop and the value in for loop, when the while loop continuously loops until a certain expression is false While loop can be very useful in situations when you may wish to execute a function until certain criterion is met. However, poor design may have your code running forever. In the case you accidentally caused an infinite loop, "ctrl+C" keys can be used to halt the execution of the code. Below are some of the symbols you can use for your statements. AND and OR operators can be also be used in combination. Symbol Function Equivalent Description s than than or equal to ater than ater than or equal to ual to to Assignment: Set your rng seed number to be equal to last 2 digits of your student ID# (Ex. ID#XXX-XX- X32->rng(32) I. Write a for loop code which wil Create column vector (with 4 random number generated using uniform distribution (rand)) Concatenate column vectors horizontally (essentially adding new columns to build the matrix. For the first loop, ignore this step) a. b. c. Repeat 10 times 2. Write a while loop code which will Create column vector (with 4 random number generated using uniform distribution (rand)) a. b. Continue looping until the sum of 4 elements of the generated column vector is greater than 2 Print the output each loop execution to verify step b