MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

More Vectors and Matrices

Contents

Entering Special Vectors

Suppose you wanted to enter a horizontal vector which has the ten entries 1,2,...,10. Would you need to type them all in? for 1,2,...,10 this wouldn't take too long but how about for 1,2,...,100? Or how about for 0,0.1,0.2,...,100?! You may ask why you'd ever need this but we'll see later in the course why we may. Suffice to say that for now there's a quick way to do this.

To get our first vector we simply type:

A=[1:10]
A =

     1     2     3     4     5     6     7     8     9    10

Which tells Matlab to just go from 1 to 10 in (default) steps of 1. If we wish to go in different steps we just put the step as a middle entry. For example we can do 0,0.1,...,1 by:

A=[0:0.1:1]
A =

  Columns 1 through 7

         0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000

  Columns 8 through 11

    0.7000    0.8000    0.9000    1.0000

Now be careful. If you do a very large vector you might wish to suppress the output so that your screen doesn't fill up with an amazing amount of vector material.

A=[0:0.1:100];

This would return a vector with 1000 entries. We've suppressed the output with the semicolon.

Indexing: Accessing Rows, Columns and Entries

First we should clear something up. Matlab treats vectors and matrices exactly the same. A vertical vector is nothing more than a matrix with one column and a horizontal vector is nothing more than a matrix with one row. Consequently from here the term matrix can also apply to a vector. The colon operator : which we just saw is also very useful for pulling information out of matrices (and vectors!) For example let's first set up a matrix:

A=[1 2 3 4;5 6 7 8;9 10 11 12]
A =

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

Suppose we wish to pull out the second row and treat it as a horizontal vector. We can do this as follows. The 2 identifies row 2 and the colon is saying "get the entire row".

A(2,:)
ans =

     5     6     7     8

Likewise we could get the second column as a vertical vector by:

A(:,2)
ans =

     2
     6
    10

You can probably guess now (try!) what the following two commands would do:

A(2,3)
ans =

     7

A(:,:)
ans =

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

If we're simply working with a vector then we don't need both parameters to pull out an entry. For example if:

v=[1;2;3;4]
v =

     1
     2
     3
     4

Then the following two lines do exactly the same thing:

v(3,:)
ans =

     3

and

v(3)
ans =

     3

Miscellaneous

We can find the length of a vector with:

v=[1;2;3;4];
norm(v)
ans =

    5.4772

We can take the transpose:

transpose(v)
ans =

     1     2     3     4

Self-Test

  1. What would the series of Matlab commands A=[-2 3 0;1 -7 8; 0.1 2 2] and then A(:,3) do?
  2. Write down a Matlab command that would find the norm of the vector [1 2 -3 0 5].
  3. Write down the Matlab command that would define the vector [2 -1 0 3] and then find the norm by taking the square root of the matrix multiplied by its transpose.

Answers to Self-Test

Next Topic:   Function Basics