Matlab® is also a programming language that is used for mathematical calculations generally. Also, you can use general programming language features in Matlab® programming. So you can use the ‘for-end’ loop in Matlab® programming when you are calculating various kinds of mathematical situations in Matlab®. In this article, we will show you how to use the ‘For End’ loop in Matlab® programming.
How To Use ‘For End’ Loop In MatLab® Programming?
>> for a = 1:2:10 b = a+1 end b = 2 b = 4 b = 6 b = 8 b = 10 >>
We preferred to explain ‘For End’ with a very basic example to teach you the logic in Matlab®. If you want much more complex examples in the below comments, we can prepare another article about it.
As you can see above, we type ‘for’ and an ‘a’ variable. So we equalize the variable ‘a’ to a vector as shown by the green arrow above. So you need to equalize a ‘vector’ to a variable that you are using in the ‘for’ loop. At each loop, the variable ‘a’ will take each value from right to left respectively in the vector that you assigned.
So you can write your calculations inside the for-end loop. These calculations and codes will work in every loop and every value that ‘a’ will be taken. At the last element of the assigned vector, the ‘a’ variable will take the last element, and the calculation inside the ‘for code will be performed last time. After that, the for-end loop will over then the program goes on to the following codes in Matlab®.
So the last element of the vector assigned in the above example must be 9. So b = a+1; and ‘b’ is calculated according to the last value of a which is nine. So ‘b’ must be 10.
As you can see in the red box above, the values of ‘a’ and ‘b’ are like those in Matlab®.
So the general logic of the for-end loop in Matlab® programming is like that.
Do not forget to leave your comments and questions below about the for-end loop in Matlab® below.
If you want further coding examples about the for-end loop in Matlab®, inform us in the comments.
This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®
Your precious feedbacks are very important to us.