__Introduction:__**This blog post is to share one of my experiments using the For loop in MATLAB. My aim, initially, was to output a simple array from user input using the For loop. However, as the greed kicked in, I started craving for a more complex output.**

Here's a quick snippet of what my code does.

Figure I. Automatically arranged matrix |

The goal is to use a matrix to store the values of

*as it is incremented throughout the duration of the For loop and arrange by*

**x***columns at the same time.*

**y**The maximum value / final value of x is given to the For loop by way of user input. Users can be prompted for inputs using the input( ) function

Now that I have the inputs, the For loop must then be created for the magic to happen.x = input('message to prompt user with')

Typically, a For loop will begin with the For instruction and end with the end instruction

My code as shown below works on the inputs which the users provideFor //sequence to run

//operations to perform

end

Let me break down the code:for x = 1:n

curr_x=x;

if curr_x > (row*n_column)

%if n_column is 10

%checks if x is currently > its row*10

%eg: if row = 2, check if x is > 20

%if it is, increase row by 1 so that x enters the next row

row = row + 1;

end

if row > 1

curr_x = curr_x - ((row-1)*n_column);

y(row,curr_x)=x;

else

%only first n_column values will use this command

y(row,curr_x)=x;

end

end

- When the user enter's the maximum value, that value goes into the variable
*n*which defines the number of iterations of the for loop. - When the user enter's the number of integers per row, it goes into the
variable.**n_column** - Then, for each iteration, the value of x is stored into the variable
which will be used for processing.**curr_x** - Firstly, we check whether the value of
is is larger than 1 (ignoring the first if function for now) . Row is always initialized as 1 before entering the for loop. This is logical because the first**row**values will always enter the first row. So if this is the first iteration, the logical operation would result in a false and the else will be executed.**n_column** - This repeats for
iterations until**n_column**is larger than the row ***curr_x**, which, brings us back to the first if function. So, how does this happen? Assuming that**n_column**is set to 5, at the**n_column****6th**iteration,will be 6 which is larger than 1 (row) * 5 (**curr_x**). This prompts the function**n_column***row = row + 1*which basically increments the row pointer to the 2nd row and so on as the for loop iterates. - So now, going back to the second if function. We see here that row is now > 1 and thus, the else portion is ignored.
- Lets go further into the
*curr_x = curr_x - ((row-1)**function. Remember that at this point,)**n_column**has a value of 6. If we were to use the**curr_x***y(row,curr_x)=x;*again, then, the matrix created will be wrong as shown belowFigure 2. Matrix with incorrect column number. *curr_x = curr_x - ((row-1)**basically does is, it takes the current)**n_column**value and normalize it for the next row.**curr_x** - Why
*((row-1)**? row-1 is used because remember that row = 2 now, if row is used, then (row*)**n_column**) becomes 2*5 = 10 and when**n_column**- 10,**curr_x**gets -4 which is an invalid column location for a matrix.**curr_x** - This algorithm will work regardless of the number of columns desired. You can put 2,3,4,10,50,60 or 100 and it will still work provided you have a large enough maximum value.

__Demonstrations:__

Figure 3. Max values with columns that are its multiple |

Figure 4. Max value with columns that are not its multiple |

## No comments:

## Post a Comment