Logo Background

Introduction to Vectors in Matlab

  • Written by matlabtutorialsmatlabtutorials No Comments Comments
    Last Updated: December 4, 2009

    Defining a Vector
    vek
    is a software package that makes it easier for you to enter matrices and , and manipulate them. The interface follows a language that is designed to look a lot like the notation use in linear algebra. In the following tutorial, we will discuss some of the basics of working with .

    If you are running windows or Mac OSX, you can start by choosing it from the menu. To start on a unix system, open up a unix shell and type the command to start the software: . This will start up the software, and it will wait for you to enter your commands. In the text that follows, any line that starts with two greater than signs (>>) is used to denote the command line. This is where you enter your commands.

    Almost all of ’s basic commands revolve around the use of . A vector is defined by placing a sequence of numbers within square braces:

    1
    2
    3
    4
    5
    
    >> v = [3 1]
     
    v =
     
         3     1

    This creates a row vector which has the label “v”. The first entry in the vector is a 3 and the second entry is a 1. Note that printed out a copy of the vector after you hit the enter key. If you do not want to print out the result put a semi-colon at the end of the line:

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

    If you want to view the vector just type its label:

    1
    2
    3
    4
    5
    
    >> v 
     
    v =
     
         3     1

    You can a vector of any size in this manner:

    1
    2
    3
    4
    5
    
    >> v = [3 1 7 -21 5 6]
     
    v =
     
         3     1    7    -21    5    6

    Notice, though, that this always creates a row vector. If you want to create a column vector you need to take the transpose of a row vector. The transpose is defined using an apostrophe (“‘”):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    >> v = [3 1 7 -21 5 6]'
     
    v =
     
         3
         1
         7
       -21
         5
         6

    A common task is to create a large vector with numbers that fit a repetitive pattern. can a set of numbers with a common increment using colons. For example, to a vector whose first entry is 1, the second entry is 2, the third is three, up to 8 you enter the following:

    1
    2
    3
    4
    5
    
    >> v = = [1:8]
     
    v =
     
         1     2     3     4     5     6     7     8

    If you wish to use an increment other than one that you have to the start number, the value of the increment, and the last number. For example, to a vector that starts with 2 and ends in 4 with steps of .25 you enter the following:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    >> v = [2:.25:4]
     
    v =
     
      Columns 1 through 7
     
        2.0000    2.2500    2.5000    2.7500    3.0000    3.2500    3.5000
     
      Columns 8 through 9
     
        3.7500    4.0000

    Accessing elements within a vector

    You can view individual entries in this vector. For example to view the first entry just type in the following:

    1
    2
    3
    4
    5
    
    >> v(1)
     
    ans =
     
         2

    This command prints out entry 1 in the vector. Also notice that a new variable called ans has been created. Any time you perform an action that does not include an assignment will put the label ans on the result.

    To simplify the creation of large , you can a vector by specifying the first entry, an increment, and the last entry. will automatically figure out how many entries you need and their values. For example, to create a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following line:

    1
    2
    3
    4
    5
    
    >> 0:2:8
     
    ans =
     
         0     2     4     6     8

    also keeps track of the last result. In the previous example, a variable “ans” is created. To look at the transpose of the previous result, enter the following:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    >> ans'
     
    ans =
     
         0
         2
         4
         6
         8

    To be able to keep track of the you create, you can give them names. For example, a row vector v can be created:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    >> v = [0:2:8]
     
    v =
     
         0     2     4     6     8
     
    >> v
     
    v =
     
         0     2     4     6     8
     
    >> v;
    >> v'
     
    ans =
     
         0
         2
         4
         6
         8

    Note that in the previous example, if you end the line with a semi-colon, the result is not displayed. This will come in handy later when you want to use to work with very large systems of equations.

    will allow you to look at specific parts of the vector. If you want to only look at the first three entries in a vector you can use the same notation you used to create the vector:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    >> v(1:3)
     
    ans =
     
         0     2     4
     
    >> v(1:2:4)
     
    ans =
     
         0     4
     
    >> v(1:2:4)'
     
    ans =
     
         0
         4

    Basic operations on

    Once you master the notation you are free to perform other operations:

    1
    2
    3
    4
    5
    
    >> v(1:3)-v(2:4)
     
    ans =
     
        -2    -2    -2

    For the most part follows the standard notation used in linear algebra. We will see later that there are some extensions to make some operations easier. For now, though, both addition subtraction are defined in the standard way. For example, to a new vector with the numbers from 0 to -4 in steps of -1 we do the following:

    1
    2
    3
    4
    5
    6
    
    >> u = [0:-1:4]
    u = [0:-1:-4]
     
    u =
     
         0    -1    -2    -3    -4

    We can now add u and v together in the standard way:

    1
    2
    3
    4
    5
    
    >> u+v
     
    ans =
     
         0     1     2     3     4

    Additionally, scalar multiplication is defined in the standard way. Also note that scalar division is defined in a way that is consistent with scalar multiplication:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    >> -2*u
     
    ans =
     
         0     2     4     6     8
     
    >> v/3
     
    ans =
     
             0    0.6667    1.3333    2.0000    2.6667

    With these definitions linear combinations of can be easily defined and the basic operations combined:

    1
    2
    3
    4
    5
    
    >> -2*u+v/3
     
    ans =
     
             0    2.6667    5.3333    8.0000   10.6667

    You will need to be careful. These operations can only be carried out when the dimensions of the allow it. You will likely get used to seeing the following error message which follows from adding two whose dimensions are different:

    >> u+v’
    ??? Error using ==> plus
    Matrix dimensions must agree.