{"id":1102,"date":"2022-04-06T12:37:11","date_gmt":"2022-04-06T11:37:11","guid":{"rendered":"https:\/\/statanalytica.com\/blog\/?p=1102"},"modified":"2022-07-18T13:54:51","modified_gmt":"2022-07-18T12:54:51","slug":"loops-in-matlab","status":"publish","type":"post","link":"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/","title":{"rendered":"A Complete Guide on Loops in Matlab With Relevant Examples"},"content":{"rendered":"\n<p>Writing the same thing, again and again, might frustrate you. This is also applicable in programming. There are several situations when a programmer needs to execute a coding program several times.<\/p>\n\n\n\n<p>These program statements are sequentially executed, which means it will first run the first statement, then the second, and so on. Programming languages offer several control structures that grant the execution of complicated programs. This is the same in the case of &#8220;MATLAB For Loop&#8221;.<\/p>\n\n\n\n<p>Well, Matrix Laboratory or MATLAB is a well-known programming language and multi-paradigm computer environment. It is developed by Mathworks. MATLAB connects computation, visualization, and programming in a simple, usable environment. It is all stated in mathematical equations.<\/p>\n\n\n\n<p>Thus, in this blog, we will study MATLAB &#8220;for&#8221; loop and &#8220;while&#8221; loop. But before discussing them, we will explore some uses of MATLAB.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"uses-of-matlab\"><\/span>Uses Of MATLAB<span class=\"ez-toc-section-end\"><\/span><\/h2><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2f3a3e852cc\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ff5104;color:#ff5104\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ff5104;color:#ff5104\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2f3a3e852cc\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#uses-of-matlab\" >Uses Of MATLAB<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#how-do-loops-in-matlab-help-you\" >How Do Loops In MatLab Help You?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#what-are-the-types-of-loops-in-matlab\" >What Are The Types Of Loops In MatLab?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#loops-in-matlab\" >Loops in MatLab<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#while-loop-in-matlab\" >While loop in matLab<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#for-loops-in-matlab\" >For loops in MatLab<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#initval-endval\" >initval:endval:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#initval-step-endval\" >initval:step:endval<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#valarray\" >valArray<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#nested-loops-in-matlab\" >Nested loops in Matlab<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#matlab-2-d-plots-example\" >MATLAB 2-D Plots Example:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#matlab-3-d-example\" >MATLAB 3-D Example:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#which-is-better-to-use-for-loop-or-while-loop-in-matlab\" >Which Is Better To Use For Loop Or While Loop In MatLab?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#list-of-conditions-in-which-you-can-use-while-and-for-loop\" >List Of Conditions In Which You Can Use While And For Loop!<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#for-vs-while-loop-matlab-difference-you-must-know\" >For vs While Loop MatLab: Difference You Must Know<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#loop-control-statements-in-matlab\" >Loop Control Statements In MatLab<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#break-statement\" >Break statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#continue-statement\" >Continue statement<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#frequently-asked-questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#q1-what-are-loops-in-matlab\" >Q1. What are loops in Matlab?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#q2-what-is-a-for-loop-matlab\" >Q2. What is a for loop Matlab?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/statanalytica.com\/blog\/loops-in-matlab\/#q3-what-is-a-while-loop-in-matlab\" >Q3. What is a while loop in Matlab?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<p>Matlab is a mathematical computing environment. According to the Matlab website, the environment is created for subsequent uses.&nbsp;Some of the <a href=\"https:\/\/statanalytica.com\/blog\/uses-of-matlab\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/statanalytica.com\/blog\/uses-of-matlab\/\" rel=\"noreferrer noopener\">uses of MatLab<\/a> are listed below:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/fkIIBLi5830PJEdq22TqonQXHEH910N6Gmi7lAza769Liz30Hly6XrkfEurf0BhVvTmdeThS4hx6qQCYex1UqXakQizvUX_ne-VXzdSnMIRcHfeMFvqAhcGthRtODKLcTOQ9ngpL=s0\" alt=\"\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>Now, let&#8217;s start discussing the loops in MATLAB.<\/p>\n\n\n\n<p>With the help of a loop statement, the programmer can execute the statements or a group of statements several times. The loops in MATLAB can be understood through the given flow diagram:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/KZ-HEHMRr4HXPGWxnLNx8mpWuHsaZvjLu53nTYPvqFVTOXI6pp1stua5T-UR3EIKMjG9O75y0J-xSY0Uf9J0YG1hkzstCFTspdIdUF9kXyzdJNxoh1HvDwBpZxlkAUBIQ1dl1b8d=s0\" alt=\"Loop Architecture\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"how-do-loops-in-matlab-help-you\"><\/span>How Do Loops In MatLab Help You?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Loops help you write the same code repeatedly and minimize your workload to execute similar code again and again.<\/p>\n\n\n\n<p><strong><em>Suppose <\/em><\/strong>a person told you to count to 500, and you have to add 2 to the previous number. You have to start from 1, then move to the next number, +2, the next number, +2, so on. This might be troublesome for you as counting to 500 with adding 2 can be an uphill task for you.<\/p>\n\n\n\n<p>In this case, it is better to use loops in Matlab. The loops are used to perform or iterate a task number of times. This is the power of loops. This is the reason why programmers of most country sites use loops in Matlab to ease their work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"what-are-the-types-of-loops-in-matlab\"><\/span>What Are The Types Of Loops In MatLab?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Matlab offers the following kinds of loops that handle the requirement of looping a statement. Let\u2019s check all these loops in Matlab:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"loops-in-matlab\"><\/span>Loops in MatLab<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"while-loop-in-matlab\"><\/span>While loop in matLab<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>It is used to repeat the number of statements or a statement when the given condition is true. It always checks the condition of the loop body before executing it.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-6-background-color has-background\"><tbody><tr><td><strong>Syntax:<\/strong><br><br><em>while &lt;expression&gt;&nbsp;&nbsp;<\/em><br><br><em>&lt;statements&gt;<\/em><br><br><em>end<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>When an expression is taken as true, then the result of it is nonempty, and it has the nonzero element that might be real or logical numeric. Otherwise, the expression is considered to be false.<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">Input:b = 1;\nwhile ( b &lt; 10 )\nfprintf(\u2018value of b: %d\\n\u2019, b);\nb = b+1;\nEnd<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-4-background-color has-background\"><tbody><tr><td><em><strong>Output:<\/strong><\/em><br><br><em>Value of b: 1<\/em><br><em>Value of b: 2<\/em><br><em>Value of b: 3<\/em><br><em>Value of b: 4<\/em><br><em>Value of b: 5<\/em><br><em>Value of b: 6<\/em><br><em>Value of b: 7<\/em><br><em>Value of b: 8<\/em><br><em>Value of b: 9<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"for-loops-in-matlab\"><\/span>For loops in MatLab<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>It is used to execute the sequential statement a number of specific times, and it abbreviates the program, which is used to manage the loop variable.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-6-background-color has-background\"><tbody><tr><td><strong>Syntax:<\/strong><br><br><em>for index = values&nbsp;&nbsp;&nbsp;<\/em><br><br><em>&lt;program statements&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/em><br><br><em>&nbsp;\u2026end<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Before understanding the different kinds of For loop MatLab example, let&#8217;s have a look at the simple example of For loop.<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">for x = 2.0: -0.1: 1.5\n   disp(x)\nend<\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-0-color\">Output:<\/mark><\/strong><\/p>\n\n\n\n<p>&nbsp;2<\/p>\n\n\n\n<p>&nbsp;1.9000<\/p>\n\n\n\n<p>&nbsp;1.8000<\/p>\n\n\n\n<p>&nbsp;1.7000<\/p>\n\n\n\n<p>&nbsp;1.6000<\/p>\n\n\n\n<p>&nbsp;1.5000<\/p>\n\n\n\n<p>Here, the values can be of any one kind among these three values that are listed below:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"initval-endval\"><\/span>initval:endval:<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>This value function can be used to increase the index variable that is incremented by 1 from initval to endval, and this will repeat the execution of the code statements until the index has greater value than endval.<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">Input:\nfor b = 1:10 \n   fprintf(\u2018value of b: %d\\n\u2019, b);\nend<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-4-background-color has-background\"><tbody><tr><td><strong>Output:<\/strong><br><br><em>value of b: 1<\/em><br><em>value of b: 2<\/em><br><em>value of b: 3<\/em><br><em>value of b: 4<\/em><br><em>value of b: 5<\/em><br><em>value of b: 6<\/em><br><em>value of b: 7<\/em><br><em>value of b: 8<\/em><br><em>value of b: 9<\/em><br><em>value of b: 10<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"initval-step-endval\"><\/span>initval:step:endval<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>This is used to increment the index function by the step value for each iteration, and it will decrement the value if the step is negative.<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">Input:\nfor b = 2.0: -0.1: 1.0   \ndisp(b)\nend<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-4-background-color has-background\"><tbody><tr><td><strong>Output:<\/strong><br><br><em>1.90000<\/em><br><em>1.80000<\/em><br><em>1.70000<\/em><br><em>1.60000<\/em><br><em>1.50000<\/em><br><em>1.40000<\/em><br><em>1.30000<\/em><br><em>1.20000<\/em><br><em>1.100001<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"valarray\"><\/span>valArray<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>It is used to create an index of subsequent columns vector from a particular array, for instance: on the initial iteration, index = valArray (:, 1). The loop will execute for the n times, where n is considered to be the number of columns for valArray, which is given by numel(valArray, 1, :). The batch of input valArray involves a cell, string, struct, or cell array.<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">for b = [12,10,15,22,27]   \ndisp(b)\nend<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-4-background-color has-background\"><tbody><tr><td><strong>Output:<\/strong><br><br><em>12<\/em><br><em>10<\/em><br><em>15<\/em><br><em>22<\/em><br><em>27<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"nested-loops-in-matlab\"><\/span>Nested loops in Matlab<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>It is used to implement a single loop or more than one loop within other loops in Matlab. This can be done for \u201dwhile\u201d loop or \u201cfor\u201d loop statements.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-6-background-color has-background\"><tbody><tr><td><strong>Syntax:<\/strong><br><br>\u201cFor\u201d loop<br><em>for m = 1:j&nbsp;&nbsp;&nbsp;<\/em><br><em>for n = 1:k&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/em><br><em>&lt;statements&gt;;&nbsp;&nbsp;&nbsp;<\/em><br><em>end<\/em><br><em>End<\/em><br>\u201cWhile\u201d loop<br><em>while &lt;expression1&gt;&nbsp;&nbsp;&nbsp;<\/em><br><em>while &lt;expression2&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/em><br><em>&lt;statements&gt;&nbsp;&nbsp;&nbsp;<\/em><br><em>end<\/em><br><em>End<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now, let&#8217;s check the example of MatLab Nested For loop in the below section.<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">for a = 2:30   \nfor b = 2:30      \nif(~mod(a.b))          \nbreak; % if factor found, not prime      \nend    \nend   \nif(b > (a\/b))      \nfprintf(\u2018%d is prime\\n\u2019, a);   \nend\nEnd<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-4-background-color has-background\"><tbody><tr><td><strong>Output:<\/strong><br><br><em>2 is prime<\/em><br><em>3 is prime<\/em><br><em>5 is prime<\/em><br><em>7 is prime<\/em><br><em>11 is prime<\/em><br><em>13 is prime<\/em><br><em>17 is prime<\/em><br><em>19 is prime<\/em><br><em>23 is prime<\/em><br><em>29 is prime<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"matlab-2-d-plots-example\"><\/span>MATLAB 2-D Plots Example:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/oWi3bwPixTHZGNttaEOPJZLA39vY_bNar3vQCVfo-0V6KiKzq9DCXTu7bg0QrDa090HJFDIigrijmi9INpr5NSZHlp7CPye_VFHnE6L7O8FK55s4ROAAupYpR4BD-hNDCBtjkYuF=s0\" alt=\"\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/WmNwIcKGWI_4rC2auUFW0g-JjX9Tx38V_g-9UnAxcglz7bb4SFD2terl4AjV-ic4B4Psf6YixzUG26sv13Gb1q_LNl2UKAX3ODdgkjxtSqIDlSU6GAVx2RSBNtN5S3I85oHJB0--=s0\" alt=\"\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"matlab-3-d-example\"><\/span>MATLAB 3-D Example:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/2qfcB1GfaeKwnYCw1uDGpsRNLNdAJ74nzvlaKXXdxUBx-cCIfX0xrekhgCJn_609vaQGNascZVS1FRhoFfiTT8CNFkPzCgFkZJdOEUeh9oMFThHCnOdYK_uLHfy5XNtTcTTNIuwf=s0\" alt=\"\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/csMI10QWGQyfQVurlUefvjWvrzHMG6B-3FcKjr-5b1e1ZIoA180sdBpGK6WeJhKfZhicFI25W3fEfjtvaJ3ukbEdjueRUTt-JMrNK05ghHpiXRPy-MB8vrsM3BDzv6BrQt1Un5cr=s0\" alt=\"\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"which-is-better-to-use-for-loop-or-while-loop-in-matlab\"><\/span>Which Is Better To Use For Loop Or While Loop In MatLab?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There is a certain case in which the For loop is much nicer and more compatible. When the user knows the number of iterations that will be done before the loop is started.&nbsp;<\/p>\n\n\n\n<p>At the same time, the While loop is much nicer in the case when the iterations need to be determined within the loop.<\/p>\n\n\n\n<p>Let\u2019s compare two different codes in terms of while loop and for loop:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-4-background-color has-background\"><tbody><tr><td><strong>for <\/strong>k = 1:20 <br>disp(k);<br>end<\/td><td>k = 1;<br><strong>while <\/strong>k &lt;= 20 <br>disp(k); <br>k = k + 1;<br>end<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>There might be some overlaps, such as executing the iterations until the specific numbers of loops or certain criterion do not reach. In such a case, the loop is most preferred as it reflects the common character of the particular loop.<\/p>\n\n\n\n<p>So, it is easy to say that the user can use the \u201cWhile\u201d loop when he\/she does not have any idea regarding the number of iterations. On the other hand, \u201cFor\u201d loops are used when the iteration numbers are known.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"list-of-conditions-in-which-you-can-use-while-and-for-loop\"><\/span>List Of Conditions In Which You Can Use While And For Loop!<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Prefer to use a For loop while you know that the loop must execute <em>n<\/em> times.<\/li><li>Use a For loop for iterating on the columns of array.<\/li><li>Practice a while loop if requesting the user\u2019s batch of input.<\/li><li>Use a while loop to read a given file within a variable.<\/li><li>Practice a while loop method while the increased value is nonstandard.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"for-vs-while-loop-matlab-difference-you-must-know\"><\/span>For vs While Loop MatLab: Difference You Must Know<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background\"><tbody><tr><td><strong>Parameters<\/strong><\/td><td><strong>While Loop<\/strong><\/td><td><strong>For Loop<\/strong><\/td><\/tr><tr><td><strong>Declaration&nbsp;<\/strong><\/td><td>while ( condition) {statements; \/\/loop body }<\/td><td>for(initialization; condition; iteration){\/\/For loop body}<\/td><\/tr><tr><td><strong>Format<\/strong><\/td><td>At the top of the loop, there is only the facility of initialization and condition checking.<\/td><td>Condition checking, initialization, iteration statement all can be written at the loop\u2019s top.<\/td><\/tr><tr><td><strong>Condition<\/strong><\/td><td>If there is no particular condition in the while loop, it gives a compiling error.<\/td><td>If there is no condition put up within the \u2018for\u2019 loop, the loop can easily iterate infinite times.<\/td><\/tr><tr><td><strong>Use<\/strong><\/td><td>The \u2018while\u2019 loop is practiced only if the number of iterations is not specifically known.<\/td><td>The \u2018for\u2019 loop is practiced when we already know the number of iterations.<\/td><\/tr><tr><td><strong>Initialization<\/strong><\/td><td>In the while loop, if the initialization is performed during condition checking, then initialization is completed every time the loop iterates.<\/td><td>Within the \u2018for\u2019 loop, once the initialization is done, it will never be repeated.<\/td><\/tr><tr><td><strong>Iteration statement<\/strong><\/td><td>In the \u2018while\u2019 loop, the user can write the iteration statement anywhere within the loop.<\/td><td>In the \u2018for\u2019 loop, the iteration statement will be written at the top. Therefore, it will be executed once all statements in the loop are executed.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"loop-control-statements-in-matlab\"><\/span>Loop Control Statements In MatLab<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It is used to change the execution of the normal sequences. Whenever an execution leaves a loop, the elements of the objects will destroy the scope of that particular object. There are two different types of control statements in Matlab:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"break-statement\"><\/span>Break statement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is used to terminate the execution of a while or for loops in Matlab. The statements that are defined after the break statement will not get executed. Whereas, in the nested loops, it exists from a specific infinite loop in which it has occurred. The control of the statement is passed to the end of the given loop.<\/p>\n\n\n\n<p><strong>Flow diagram<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/fy8e_JfTuunhNMXUmdZZqhE7CjAPtMHWlvF0lPL_BFadbGlNvuLPtxut50d5B7st7sYEbgqWRgkGP2tZe6UH35L9rjTi7uliCywVyDVdO3wMEQU5eR0XirFcxGvIp2TX95oInUR5=s0\" alt=\"MATLAB break statement\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">b = 1;\nwhile (b &lt; 10 )   \nfprintf(\u2018value of b: %d\\n\u2019, b);   \nb = b + 1;      \nif( b > 5)         \nbreak;      \nend \nend<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-4-background-color has-background\"><tbody><tr><td><strong>Output:<\/strong><br><br><em>value of b: 1<\/em><br><em>value of b: 2<\/em><br><em>value of b: 3<\/em><br><em>value of b: 4<\/em><br><em>value of b: 5<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"continue-statement\"><\/span>Continue statement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This statement can pass the control to the upcoming or next iteration in a while or for loops in Matlab. Somehow, it also works as a break statement. Rather than forcing the termination from the loop, it moves to the next iteration of the given loop, and it skips any of the codes in between the program.<\/p>\n\n\n\n<p><strong>Flow diagram:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/SOlYWQw6wpqrPPW0kGn2P4QWS_-4KC1HWKIv3ApvDEYHha2_i57wtu1NFL-RYPj5CerpKxfpx3Yr0o90jY6zXISipMiQ6oPXXspu2CBp4be2-Xi5iJ8SisPCJAPSu85U7jAgm-2-=s0\" alt=\"MATLAB continue statement\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">b = 1;\nwhile b &lt; 11   b = b + 1;    \nif b == 5      \ncontinue;   \nend \nfprintf(\u2018value of b: %d\\n\u2019, b);\nend<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-ast-global-color-4-background-color has-background\"><tbody><tr><td><strong>Output:<\/strong><br><br><em>value of b 1<\/em><br><em>value of b: 2<\/em><br><em>value of b: 3<\/em><br><em>value of b: 4<\/em><br><em>value of b: 6<\/em><br><em>value of b: 7<\/em><br><em>value of b: 8<\/em><br><em>value of b: 9<\/em><br><em>value of b: 10<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Matlab grants the user to use the various kinds of loops in Matlab programming that are used to handle different looping requirements that involve: while loops, for loops, and nested loops.&nbsp;<\/p>\n\n\n\n<p>Besides these, it also has two different control statements that are: break statement and continue statement, which is used to control the looping of the statement in a program.&nbsp;Using the loops for specific repetitions statements can be a great way to shorten the final coded program. <\/p>\n\n\n\n<p>If you have any difficulties related to assignments of Matlab, then you can avail of services for <a href=\"https:\/\/statanalytica.com\/matlab-assignment-help\">Matlab homework help<\/a> and <a href=\"https:\/\/statanalytica.com\/matlab-assignment-help\">Matlab assignment help UK<\/a>. We have experts team that can be accessible 24\/7 with high-quality data. The services are offered at minimal prices, and we can provide you instant help that will deliver the assignments before the deadlines. So, avail of our services, and get relaxed from the complicated Matlab assignments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"frequently-asked-questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-61175116783bf\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"q1-what-are-loops-in-matlab\"><\/span>Q1. <strong>What are loops in Matlab?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A loop statement enables the user to execute a group of statements or a statement several times. Matlab offers different types of loops to manage looping necessities, including for loops, while loops, and nested loops.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-61175116783c1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"q2-what-is-a-for-loop-matlab\"><\/span>Q2. <strong>What is a for loop Matlab?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A For loop is used for repetition control structure, enabling the user to write a loop efficiently that requires to perform a specific number of times. The syntax for the &#8216;for&#8217; loop in MATLAB is as: for index = values.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-61175116783c2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"q3-what-is-a-while-loop-in-matlab\"><\/span>Q3. <strong>What is a while loop in Matlab?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The while loop executes the program statement(s) repeatedly as long as the condition remains true. A condition is true till the output is nonempty and includes all nonzero components (real numeric or logical). Otherwise, the condition is false.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Writing the same thing, again and again, might frustrate you. This is also applicable in programming. There are several situations when a programmer needs to execute a coding program several times. These program statements are sequentially executed, which means it will first run the first statement, then the second, and so on. Programming languages offer [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":1108,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[138],"tags":[],"class_list":["post-1102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/posts\/1102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/comments?post=1102"}],"version-history":[{"count":0,"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/posts\/1102\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/media\/1108"}],"wp:attachment":[{"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/media?parent=1102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/categories?post=1102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statanalytica.com\/blog\/wp-json\/wp\/v2\/tags?post=1102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}