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 x as it is incremented throughout the duration of the For loop and arrange by y columns at the same time.
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 n_column variable.
- Then, for each iteration, the value of x is stored into the variable curr_x which will be used for processing.
- Firstly, we check whether the value of row 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 n_column 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.
- This repeats for n_column iterations until curr_x is larger than the row * n_column, which, brings us back to the first if function. So, how does this happen? Assuming that n_column is set to 5, at the 6th iteration, curr_x will be 6 which is larger than 1 (row) * 5 (n_column). This prompts the function 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)*n_column) function. Remember that at this point, curr_x has a value of 6. If we were to use the y(row,curr_x)=x; again, then, the matrix created will be wrong as shown below
Figure 2. Matrix with incorrect column number. - Why ((row-1)*n_column)? row-1 is used because remember that row = 2 now, if row is used, then (row*n_column) becomes 2*5 = 10 and when curr_x - 10, curr_x gets -4 which is an invalid column location for a matrix.
- 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.
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