Lab 1: Vectors and Matrices - Wednesday, 2016-01-27

This lab will focus on the instantiation and manipulation of vectors and matrices. When you have completed this exercise, please upload your published work to your cs130r directory.

Contents

Getting Started

You are currently working in a MATLAB m-file. Every line that begins with a % is called a comment. Comments are meant to be read by programmers, and are not interpreted by MATLAB. Comments that start with %% indicate the start of a new code Section. Each code Section can be run independently without having to run any other pieces of code. The location of your cursor will determine which code Section is active, and that section will be highlighted in yellow. Try running this code section by placing your cursor on the code below and clicking the "Run Section" button in the toolbar above.

disp('Success');
Success

You should now see the string 'Success' printed in your Command Window, but not any of the output from the Sections below.

Creating Row Vectors

Row vectors are created by enclosing comma- or space-delimited numbers in square brackets. Assign a row vector with the numbers 5, 6, 7, and 8 to the variable rvec. To make your code more readable, I recommend making your vectors assignments comma-delimited.

rvec = [5, 6, 7, 8]
rvec =

     5     6     7     8

If you are making a row vector from a sequence of numbers, you can use the colon operator to make your life easier. To get a sequence of numbers from -1 to 3, you can use -1:3. Try making the same row vector rvec using the colon operator.

rvec = 5:8
rvec =

     5     6     7     8

With the previous use of the colon operator, MATLAB assumed that you wanted your sequence to be spaced by 1. Use the help function to see if you can determine the syntax for creating differently spaced sequences of numbers. Now, assign a row vector rvec with values ranging from 20 to 30 in steps of 2.

rvec = 20:2:30
rvec =

    20    22    24    26    28    30

Creating Column Vectors

Column vectors are made in a similar way to row vectors, but each new row is delimited by a semicolon. Assign a column vector with the numbers 1, 3, 15, and 42 to the variable cvec.

cvec = [1; 3; 15; 42]
cvec =

     1
     3
    15
    42

We can also create a column vector by taking the transpose of a row vector. Used the help function to learn how to use the transpose operator. Now, create a new column vector called cvec by taking the transpose of row vector containing the number 5, 2, and 93.

cvec = [5, 2, 92].'
cvec =

     5
     2
    92

Creating Matrices

To create a matrix, we need only combine the tools we learned for row and column vectors (i.e. columns are separated by commas or spaces and rows are separated by semicolons). Create a 2x2 matrix called mat using any values you choose.

mat = [4, 5; 12, 8]
mat =

     4     5
    12     8

Indexing

Now that we have the tools to make vectors and matrices, let's see how we can manipulate them. Indexing allows us to access and modify parts of an array. Each position of a vector is sequentially numbered starting from 1. Use the doc function to learn the syntax for matrix indexing. Create a 4x3 matrix containing only zeros. Then, use indexing to assign the value 6 to position (3,2).

mat = zeros(4,3)
mat(3,2) = 6
mat(7) = 6
mat =

     0     0     0
     0     0     0
     0     0     0
     0     0     0


mat =

     0     0     0
     0     0     0
     0     6     0
     0     0     0


mat =

     0     0     0
     0     0     0
     0     6     0
     0     0     0

MATLAB has a convenient method for replace entire rows or columns in a single operation. Using the colon operator, replace row 1 of mat with the value 2.

mat(1,:) = 2
mat =

     2     2     2
     0     0     0
     0     6     0
     0     0     0

Adding Elements

Indexing can also be used to add elements to an existing vector or matrix. Make a 4 element column vector called addvec. Add an element to the end that contains the value 8. You can do this by indexing the 5th element.

addvec = [2, 6, 31, 4].'
addvec(5) = 8
addvec =

     2
     6
    31
     4


addvec =

     2
     6
    31
     4
     8

Deleting Elements

What if we want to remove the element we just added to addvec? Well, we can remove it by replacing the 5th element with an empty vector. Use the doc function to learn the syntax for an empty vector. Then, remove the 5th element of addvec.

addvec(5) = []
addvec =

     2
     6
    31
     4

Indexing with Vectors

Changing elements one-by-one can be tedious if you need to make many modifications. Fortunately, you can use vectors to index multiple elements at the same time. Create a 4x4 matrix of zeros called modmat. Replace the elements (2,1), (2,2) and (2,3) with the value 3. (Hint: replace the column index with a vector)

modmat = zeros(4)
modmat(2,1:3) = 3
modmat =

     0     0     0     0
     0     0     0     0
     0     0     0     0
     0     0     0     0


modmat =

     0     0     0     0
     3     3     3     0
     0     0     0     0
     0     0     0     0

There are many situations where we don't know how many elements a vector contains, but we need to access a subset of the elements. Assume you have a vector of unknown length called mystery, use the built-in expression end to assign the last 8 values of mystery to a new vector called subset.

mystery = 1:100;
subset = mystery(end-7:end)
subset =

    93    94    95    96    97    98    99   100

Logical Indexing

Sometimes we want to get a subset of a vector or matrix rather than to modify elements. In this case, we can use logical indexing. By indexing a vector with a logical vector of the same size, we can access all of the elements in the vector that correspond to values of true in the logical vector. Make a 5 element row vector of number from 1 to 5 called bigset. Make another 5 element vector with the sequence [0, 1, 0, 0, 1] called logicidx, and cast it as logical. Now create another vector called smallset by indexing bigset with logicidx.

bigset = 1:5
logicidx = logical([0, 1, 0, 0, 1])
smallset = bigset(logicidx)
bigset =

     1     2     3     4     5


logicidx =

     0     1     0     0     1


smallset =

     2     5

Dimensions

Determining the number of elements of a vector or matrix is important for many debugging purposes. MATLAB has several functions for determining the dimensions of vectors and matrices. Create a 1x5 row vector called rvec, a 3x1 column vector called cvec, and a 7x4 matrix called mat. Try to figure out the different information returned by the function size, length, and numel. When are they the same? When are they different?

rvec = 1:5
cvec = (1:3).'
mat = randi(10,7,4)

fprintf('Row length is %d\n', length(rvec))
fprintf('Row size is %d x %d\n', size(rvec))
fprintf('Row numel is %d\n', numel(rvec))

fprintf('Column length is %d\n', length(cvec))
fprintf('Column size is %d x %d\n', size(cvec))
fprintf('Column numel is %d\n', numel(cvec))

fprintf('Matrix length is %d\n', length(mat))
fprintf('Matrix size is %d x %d\n', size(mat))
fprintf('Matrix numel is %d\n', numel(mat))
rvec =

     1     2     3     4     5


cvec =

     1
     2
     3


mat =

     9     6     9     1
    10    10     2     9
     2    10     5    10
    10     2    10     7
     7    10     8     8
     1    10    10     8
     3     5     7     4

Row length is 5
Row size is 1 x 5
Row numel is 5
Column length is 3
Column size is 3 x 1
Column numel is 3
Matrix length is 7
Matrix size is 7 x 4
Matrix numel is 28

Changing Dimensions

MATLAB has plenty of function for altering the dimensions of vectors and matrices. You can flip (fliplr and flipud), rotate (rot90, .'), or change dimensions (reshape). Create a 4x4 matrix called sqmat containing the sequence of values from 1 to 16 (top to bottom then left to right). Create a 2x8 matrix called newmat by changing the dimensions of sqmat.

sqmat = [1:4;5:8;9:12;13:16].'
newmat = reshape(sqmat,2,8)
sqmat =

     1     5     9    13
     2     6    10    14
     3     7    11    15
     4     8    12    16


newmat =

     1     3     5     7     9    11    13    15
     2     4     6     8    10    12    14    16

Challenge Questions

If you are feeling comfortable with the material, feel free to try the next few challenge questions.

Make a 5x8 matrix of random integers from 1 to 10. Using the tools you've learned, unwind (column-wise) the matrix into a 40x1 column vector.

mat = randi(10, 5, 8)
mat(:)
mat =

     7     1    10     8     8     2     6     7
     2     1     1     2     8     2     3     9
     8     9     5     5     3     5     8    10
     1     7     4     5     7    10     3     6
     3     4     8     7     7     4     6     2


ans =

     7
     2
     8
     1
     3
     1
     1
     9
     7
     4
    10
     1
     5
     4
     8
     8
     2
     5
     5
     7
     8
     8
     3
     7
     7
     2
     2
     5
    10
     4
     6
     3
     8
     3
     6
     7
     9
    10
     6
     2

The code below reads and displays an image. Insert one line of code between the two comments to swap the red channel (mxnx1) with the blue channel (mxnx3). (Hint: use a semicolon to suppress your output)

im = imread('ngc6543a.jpg');
%insert code below
im = flip(im,3);
%insert code above
image(im)