Lab 2
Using Arrays and Variables

CS211 Lab Policy:

Lab Background:

For this lab you will work in MATLAB's command window creating variables.  Once you have created all of the variables, you will save your MATLAB workspace to the file lab2.mat and submit that file to the course web site.

Instructions:

  1. Start MATLAB and type whos at the command prompt (>>) to verify that you have no variables saved in your workspace.  If whos lists any variables, type clear .  Typing whos again should show no variables.
     
  2. Create the row vector with all of the numbers from 1 to 10 and assign these values to a variable called One_to_ten.  It should look like the following:

    1    2    3    4    5    6    7    8    9    10

    When you type whos, you should see the name of your new variable (One_to_ten), its size (1 row by 10 columns), its size in memory (80 bytes), and its data type or class (double array).  Save your workspace to the file lab2.mat by typing

        save lab2

    Confirm that your workspace was saved correctly by clearing all variables by typing clear, then reloading your saved workspace by typing

        load lab2

    and then confirming your One_to_ten variable exists by typing whos and has the right value by typing One_to_ten
     
  3. Assign to a variable named Ten_to_one, the row vector of integers from 10 down to one.  When displayed, it should look like the following:

    10   9    8    7    6    5    4    3    2    1

    Use whos to verify that variable Ten_to_one has been stored in the workspace along with One_to_ten
     
  4. Assign to a variable named Five_evens a row vector with all the even numbers from 1 to 10.  It should look like the following:

    2    4    6    8    10
     
  5. Assign to a variable named Many_evens a row vector with all the even numbers from 1 to 1000.  Hint: don't type in more than 3 numbers to create this row vector!
     
  6. Assign to a variable named Five_odds_col a column vector with all the odd numbers from 1 to 10.  When displayed, your variable should look like the following:

    1
    3
    5
    7
    9

     
  7. Assign to a variable named Odd_column a column vector with all the odd numbers from 1 to 1000.  Hint: transpose a row vector with all of the odd numbers from 1 to 1000.
     
  8. Assign to a variable named Matrix, the 3 x 4 matrix shown below. (There are many ways to do this. Can you do it in more than one way?)

    1    2     3    4
    5    6     7    8
    9   10    11   12
     
  9. Assign to a variable named Many_zeros a 10 by 10 matrix in which each element has the value zero.
     
  10. Assign to the variable named Mostly_zeros a 10 by 10 matrix in which each element has the value zero with the following exceptions:
    1. the element in row 3, column 4 has the value 1
    2. the element in row 7, column 2 has the value 2
    3. the element in row 10, column 10 has the value 3

    When displayed, Mostly_zeros should appear as shown below.

    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0
    0    0    0    1    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0
    0    2    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    0
    0    0    0    0    0    0    0    0    0    3

     

  11. Assign to a variable named Forty_twos a 5 x 8 matrix in which each element has the value 2.  Hint:  Use ones() and a multiplication.
     
  12. Assign to a variable named Target the 10 x 10 matrix shown below.

    0 0 0 0 0 0 0 0 0 0
    0 1 1 1 1 1 1 1 1 0
    0 1 2 2 2 2 2 2 1 0
    0 1 2 3 3 3 3 2 1 0
    0 1 2 3 4 4 3 2 1 0
    0 1 2 3 4 4 3 2 1 0
    0 1 2 3 3 3 3 2 1 0
    0 1 2 2 2 2 2 2 1 0
    0 1 1 1 1 1 1 1 1 0
    0 0 0 0 0 0 0 0 0 0


    Hints:  Start out with a 10x10 matrix of all zeros.  Then change that matrix's center 8x8 matrix to all 1's.  Then change that matrix's center 6x6 matrix to all 2's, etc.  It is okay if your matrix's columns are more spread out when displayed.
     
  13. Check your workspace with whos to make sure that it contains all of the variables that you created in steps 2-12 above.  Once you have all of these variables in your workspace, save your workspace to the file lab2.mat with

        save lab2.
  14. Open your Command History window (use the Desktop menu). Highlight all the commands you entered for today's lab (select the first command, then with your shift-key held down, select the last command). Copy all these commands to your clipboard (use the Edit menu --> copy command). Open a MATLAB .m file (use the File menu --> New command --> M-File command). Paste all of your commands into the new m-file (use the Edit menu --> paste command). Save this file as lab2.m in your CS211 folder and submit it to the course web site.

Turn-in:

Submit your lab2.mat and lab2.m files as your Lab 2 turn-in.