Sunday, 17 May 2015

MATLAB: Using for loop to construct & arrange matrices


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
x = input('message to prompt user with')
Now that I have the inputs, the For loop must then be created for the magic to happen.
Typically, a For loop will begin with the For instruction and end with the end instruction

For //sequence to run

//operations to perform

My code as shown below works on the inputs which the users provide
for x = 1:n
    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;
    if row > 1
        curr_x = curr_x - ((row-1)*n_column);
        %only first n_column values will use this command
Let me break down the code:
  1. 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.
  2. When the user enter's the number of integers per row, it goes into the n_column variable.
  3. Then, for each iteration, the value of x is stored into the variable curr_x which will be used for processing.
  4. 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.
  5. 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.
  6. So now, going back to the second if function. We see here that row is now > 1 and thus, the else portion is ignored.
  7. 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.
    We see that an additional 5 columns is created which is not what I desired. So, what curr_x = curr_x - ((row-1)*n_column) basically does is, it takes the current curr_x value and normalize it for the next row.
  8. 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.
  9. 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
Due to the nature of how MATLAB handles the matrix, I see that there is a certain amount of robustness (for the current application) when the column number is not a multiple of the max value. From Figure 4, any values that are not available are instantly replaced by a 0.

No comments:

Post a Comment