Actually, for loop is a common in general programming languages avaliable. Also Matlab has for loop to obtain required algorithms. In here, we explain how to use for loop in Matlab with very basic examples.

## How To Use ‘for’ Loop In MatLab Programming?

To use for loop in Matlab, you can click on the given link to remember the operators that are used in Matlab.

You can obtain loops for different queries to obtain algoritms in Matlab, by increasing the looped value in each loop by using ‘for’ loop. Taka a look at the example below;

```
>> x = [5+3j 2+6j 5j 5 -4 0 -8j 12 -1+8j];
im = 0; real = 0;
for y = 1:9
if imag(x(y))~=0
%query for each elements of x
real=real+1;
else
real = real+1;
end
end
real
im
real =
3
im =
5
```

If you understand above program example that obtained in Matlab command window, you will learn the logic of for-end in Matlab. Assume that we want to create a program that finds the number of elements that has imaginary value and that has not imaginary value separately, of a vector in Matlab.

Assume that we have vector that has 9 elements inside it, and some of these elements has imaginary number. To do it, we created two of variables named ‘im’ and ‘real’ after the vector. These variables have initial values of 0. In general, you need to give inital values to your variables that will be used in for-end loop in Matlab. These values will change inside for-end loop.

To obtain for loop, you need to create a vector that has elements in increasing direction. For example, we created the vector ‘y’ for for-end loop above, that has elements of 1 to 9 inside it. In each loop, ‘y’ will take values starting from 1 ending at 8. Once for-end loop reaches to 9, program will exit the for-end loop, and go on other codes after the for-end loop.

To find out the imaginary element inside vector ‘x’, we created a if-else query. We said that in each loop, ‘y’ will take different values. So, if we build a program that asks to the vector ‘x’, “Does your current y. element(which is the value of ‘y’ in current loop…) has imaginary section?”

We asked this question with if-else in Matlab by indexing the each element of vector ‘x’ in each loop, inside ‘imag()’ command. If the imaginary section is not zero that we stated in if command above, the value of ‘im’ will be increased 1(im=im+1). If there is no imaginary section(so it is ‘else’ in our code), the value of ‘real’ will be increased 1.

At the end of the loop, we want to see the values of these two vectors. As you see in the result at command window, 3 of elements has no imaginary section, so they are real, and the number that has imaginary section is 5.

So in each loop, the variable that assigned to for-end command will take a new values. You can obtain programmes for these new values in each loop, inside the for-end command in Matlab.

Another example about for-end loop in Matlab;

```
x = [1 5 6 3; 4 6 3 1; 7 6 5 6;];
for y = 1:3
%indexing the rows of x matrix
for z = 1:4
%indexing the columns of x matrix
D(z,y)=x(y,z)
end
end
D =
1 4 7
5 6 6
6 3 5
3 1 6
```

This example is about the priority of the for-end loops that coded inside themselves. In here, we want to convert a 3×4 matrix which is ‘x’ into a 4×3 matrix which is ‘D’ above. So in each loop, we need to index the matrix elements individually to assign these valus to a new matrix elements.

We created a for-end loops that intertwined to each other. For the outmost for-end loop, we created a vector variable ‘y’, which will index the row of individual element from ‘x’ to assing it to matrix ‘D’ as column.

In innermost for-end loop, we created the vector variable ‘z’ to index the column adress of each elements of matrix ‘x’ to assign them into rows of matrix ‘D’.

Inside the innermost for-end loop, we wrote the code that will exchange the elements of matrix ‘x’ into matrix ‘D’, changing their row and column adresses to column and row adresses in ‘D’.

The created code will start the taking the value of 1 for ‘y’, and taking the value of 1 for ‘z’. The inner for-end loop has the priority; which will increase the value of ‘z’ up to 4, when the value of ‘y’ is 1. When the inner loop complete, outer loop will to ‘y’ the value of 2. So, code will work in this manner up to ‘y’ reaches the value of 4.

As you see in the result, matrix ‘x’ inverted to matrix ‘D’ in command window in Matlab.

YOU CAN LEARN MatLab IN MECHANICAL BASE; Click And Start To Learn MatLab!

If you want further code examples about for-end loop in Matlab, state it in comments.

Do not forget to leave your comments and questions about for-end loop in Matlab below.

Your precious feedbacks are very important for us.