Lab 20

Multidimensional Arrays and Sparse Arrays

CS211 Lab Policy:

Instructions:

For this lab you will create the function lab20() saved in file lab20.m along with one user-defined sub-function also saved in lab20.m.  Add code to your function that performs the following:

  1. Define a sub-function named box() that has 3 input arguments and one output argument.  The input arguments represent the number of rows, columns, and layers of the 3-D matrix created by box and returned as the output argument.  All internal elements of the 3-D matrix returned by box must be zeros and all external elements (those in the first or last row, first or last column, or first or last layer), must be 1.  For example, the function call:

    box(3, 4, 3)

    should return the matrix:

    (:,:,1) =

    1 1 1 1
    1 1 1 1
    1 1 1 1

    (:,:,2) =

    1 1 1 1
    1 0 0 1
    1 1 1 1

    (:,:,3) =

    1 1 1 1
    1 1 1 1
    1 1 1 1

    Add the box function definition as a sub-function at the end of lab20.m.  Add to your lab20() function (the main program) code that does the following.
     

    1. get from the user a number of rows

    2. get from the user a number of columns

    3. get from the user a number of layers

    4. call box with the user inputs as its 3 input arguments

    5. display the 3-D matrix returned by box (using disp)
       

  2. Create a multidimensional array called MPA that will store and organize cadet MPA's ratings according to the following scheme:

    1. The first index will vary from 1-40 and represent which squadron the cadet is in.

    2. The second index will vary from 1-4 and represent which class (firstie, 2-degree, 3-degree, 4-degree) the cadet is in.

    3. The third index will vary from 1-20 and specify which element the cadet is in.

    4. The fourth index will vary from 1-10 and uniquely identify the cadet within their element.

    This 4-dimensional array requires that 4 values be used to access an individual cadet's MPA rating. For example,

        MPA(18, 2, 3, 5) would store the value for a cadet in squadron 18, who is the 5th cadet in element 3 and is a 2-degree.
     
    Assign a random value in the range [0.0,4.0] to each element of the MPA array.

    Print out all the MPA rating values for your squadron from this array.

    Print out all the MPA rating values for every 2-degree in the 2nd group.

    Add a comment to your code that states whether this organization of MPA values into a 4-dimensional array makes sense to you. Would you rather store all the values in a single-dimensional vector? Why or why not?

     

  3. Add code to your lab20() function that will do the following. For each step create a unique variable.
    1. Create a sparse matrix containing random values with 5,000 rows and 5,000 columns and a density of 5% (0.05).
    2. Create a second sparse matrix of the same size and density.
    3. Call the tic() function to start a "stop watch."
    4. Multiply the two matrices together to produce their product.
    5. Call the toc() function and print its value. This is the length of time, in seconds, it took to perform the multiplication.
       
    6. Convert the matrix in step a above to a full matrix using the full() command.
    7. Convert the matrix in step b above to a full matrix.
    8. Call the tic() function.
    9. Multiply the two full matrices together to create their product.
    10. Call the toc() function and print its value.

      Compare the length of time it took to multiple the two different types of matrices. The results of these operations on Dr. Brown's computer are shown below. Your exact times may vary significantly, but the percentage difference between your times should be similar.

      It took 10.725000 seconds to multiply the sparse matrices
      It took 196.052000 seconds to multiply the full matrices


      Note: If your get an error message like:
          ??? Error using ==> horzcat
          Out of memory. Type HELP MEMORY for your options.

      make the sizes of your sparse arrays in step a and b smaller until the "Out of memory" error is no longer generated.
       
  4. Add code to your lab20() function that will display information about the variables you created for step 3 above. (Call the whos function.) Compare the data type and amount of memory used for each variable. Notice the amount of memory that is saved by the sparse matrices.

    Place one or more lines of comments at the bottom of your lab20()function and document the memory usage for your variables and the percentage of memory that was saved by the sparse matrices.

Turn-in:

Submit your lab20.m file.