Program (1): To print number 2 four times. MATLAB while loop executes statements repeatedly an indefinite number of times as long as expression(1) evaluates to true condition. (The 1:n version is a normal case of this, because in Matlab 1:n. otherrowvector 4, 3, 5, 1, 2 for anyname otherrowvector display (anyname) end. The for loop assigns a different element of this vector to the variable each run. Program (2): To print a table of any number, enter by user. The left-hand side of the assignment can be any valid variable name. for k=1:2Ĭreate a script file in MATLAB and type the following code – Program (1): To print number 5 two times. Matlab offers different types of loops to manage looping. MATLAB for loop first initialize the variable with the initial value then execute statements after that increment the variable value by 1 and do this again and again until the variable value reaches to the final value. A loop statement enables the user to execute a group of statements or a statement several times. General Form: for variable = initial value : final value MATLAB for loop executes statements a specific number of times. MATLAB loops are generally used for executing the block of statements repeatedly along with tracking of each iteration with an incrementing index variable. That’s why MATLAB provides us, a repetitive structure called loop. In real life, many times we need to perform some task repeated over and over, until a specific goal is reached. After reading this MATLAB Loop topic, you will understand loop types and you will know the for and while loops theory, and examples.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |