EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial For Loop in Matlab
 

For Loop in Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated July 3, 2023

For Loop in Matlab

 

 

Introduction to For Loop in Matlab

MATLAB provides its user with a basket of functions; in this article, we will understand a powerful element called ‘For loop.’ For loop is a conditional iterative statement used in programming languages. It checks for desired conditions and then executes a block of code repeatedly. The code block is implemented as long as those defined conditions are met. An explicit loop counter distinguishes the ‘for loop’ from other looping statements. This is also referred to as the loop variable; this allows the loop body to know the sequencing of every iteration. In this topic, we will learn about For Loop in Matlab.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of For Loop:

for index = value/values
statement
end

Now let us understand the ‘for loop’ in detail.

Examples of For Loop in Matlab

For index = It will include values, single or multiple statements, and end

This function will run a defined set of statements in the loop for the number of times specified in the condition

Values can have a number of forms, e.g.:

  • firstVal: lastVal: it will gradually increase the index by 1 from firstval till lastval; it will run the set of statements till firstVal is greater than the lastVal.
  • firstVal: step : lastVal: it will gradually increase the index by the defined “step” value or decrease the value by “step” for negative values.
  • valArray: Each iteration will generate a column vector from columns of the array for valArray.

Now let us use ‘for loop’ in some examples:

Decrement Values

It will decrement the values by the defined interval.

Code:

for a = 2.0 : -0.5 : 0.0
disp(a)
end

Here, our output will be decremented by ‘0.5.’

Output:

For Loop in Matlab output 1

Let’s take a different decrement interval

Code:

for a = 3.0 : -1.0 : 0.0
disp(a)
end

Here is the output that we will get; as we can notice, the values are decremented by 1:

Output:

For Loop in Matlab output 2

Increment Values

It will increment the values by the defined interval.

Code:

for a = 6.0 : 1.0 : 9.0
disp(a)
end

Here is the output that we will get; as we can notice, the values are incremented by 1:

For Loop in Matlab output 3

Let’s take a different decrement interval:

Code:

for a = 4.0 : 2.0 : 9.0
disp(a)
end

Here is the output that we will get; as we can see, the values are incremented by 2:

Output:

For Loop in Matlab output 4

Specified Values

It will execute statements for specified values

Code:

for a = [1 3 8 7] disp(a)
end

This is how our output will look like:

Output:

output 5

As we can notice, the values are printed.

Use of Repeat Statement for every Matrix Column

Here the eye is a 2X3 Identity matrix

Code:

for I = eye (2,3)
disp('Current value:')
disp(I)
end

This is how our output will look like:

Output:

output 6

Use of BREAK Statement

To exit the ‘for loop,’ we can use the break statement. The following example will print the ‘END’ value without a break after every iteration.

Code:

for I = eye (3)
disp(‘Value:’)
disp(I)
disp(‘END’)
end

This is how our output will look like:

Output:

output 7

Using it with a break statement will break the ‘for loop’ after the first iteration.

Code:

for I = eye (3)
disp(‘Value:’)
disp(I)
break
disp(‘END’)
end

Output:

output 8

As we can notice, our loop ended after the first iteration.

Conclusion

  • To exit the loop using code, we can make use of a break
  • If we want to skip forward coming instructions in ‘for loop’ & begin the next iteration, we can make use of continue
  • If we iterate over single-column vector values, we can first take its transpose to create a row vector.

Recommended Articles

This is a guide to For Loop in Matlab. Here we discuss For Loop in Matlab, appropriate syntax, and respective examples. You may also have a look at the following articles to learn more –

  1. Boxplot in Matlab
  2. Grid on Matlab
  3. Matlab Gradient
  4. Matlab Logical Operators
  5. fminsearch in Matlab | Examples

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW