Skip to content Skip to navigation

Connexions

You are here: Home » Content » An Introduction to MATLAB: Vectors and Matrices

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Endorsed by Endorsed (What does "Endorsed by" mean?)

This content has been endorsed by the organizations listed. Click each link for a list of all content endorsed by the organization.
  • IEEE-SPS

    This module is included inLens: IEEE Signal Processing Society Lens
    By: IEEE Signal Processing SocietyAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "IEEE-SPS" link to see all content they endorse.

  • College Open Textbooks display tagshide tags

    This module is included inLens: Community College Open Textbook Collaborative
    By: CC Open Textbook CollaborativeAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "Reviewer's Comments: 'I recommend this book as a "required primary textbook." This text attempts to lower the barriers for students that take courses such as Principles of Electrical Engineering, […]"

    Click the "College Open Textbooks" link to see all content they endorse.

    Click the tag icon tag icon to display tags associated with this content.

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • Bookshare

    This module is included inLens: Bookshare's Lens
    By: Bookshare - A Benetech InitiativeAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "Accessible versions of this collection are available at Bookshare. DAISY and BRF provided."

    Click the "Bookshare" link to see all content affiliated with them.

  • NSF Partnership display tagshide tags

    This module is included inLens: NSF Partnership in Signal Processing
    By: Sidney BurrusAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "NSF Partnership" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Featured Content display tagshide tags

    This module is included inLens: Connexions Featured Content
    By: ConnexionsAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "A First Course in Electrical and Computer Engineering provides readers with a comprehensive, introductory look at the world of electrical engineering. It was originally written by Louis Scharf […]"

    Click the "Featured Content" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • UniqU content

    This module is included inLens: UniqU's lens
    By: UniqU, LLCAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "UniqU content" link to see all content selected in this lens.

  • Lens for Engineering

    This module is included inLens: Lens for Engineering
    By: Sidney Burrus

    Click the "Lens for Engineering" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

An Introduction to MATLAB: Vectors and Matrices

Module by: Louis Scharf. E-mail the author

Note:

This module is part of the collection, A First Course in Electrical and Computer Engineering. The LaTeX source files for this collection were created using an optical character recognition technology, and because of this process there may be more errors than usual. Please contact us if you discover any errors.

As its name indicates, MATLAB is especially designed to handle matrices. The simplest way to enter a matrix is to use an explicit list. In the list, the elements are separated by blanks or commas, and the semicolon (;) is used to indicate the end of a row. The list is surrounded by square brackets [][]. For example, the statement

≫ A = [1 2 3;4 5 6;7 8 9]

results in the output

A = 
   1 2 3
   4 5 6
   7 8 9

The variable A is a matrix of size 3×33×3. Matrix elements can be any MATLAB expression. For example, the command

≫ x = [-1.3 sqrt(3) (1+2+3)*4/5]

results in the matrix

x = 
   -1.3000  1.7321  4.8000

We call a matrix with just one row or one column a vector, and a 1×11×1 matrix is a scalar. Individual matrix elements can be referenced with indices that are placed inside parentheses. Type x(5) = abs(x(1)) to produce the new vector

x = 
   -1.3000  1.7321  4.8000  0.000  1.3000

Note that the size of x has been automatically adjusted to accommodate the new element and that elements not referenced are set equal to 0 (here x(4)x(4)). New rows or columns can be added very easily. Try typing r = [10 11 12],A = [A;r]. Dimensions in the command must coincide. Try r = [13 14],A = [A;r].

The command size(A) gives the number of rows and the number of columns of A. The output from size(A) is itself a matrix of size 1×21×2. These numbers can be stored if necessary by the command [m n] = size(A). In our previous example, A = [A;r] is a 4×34×3 matrix, so the variable m will contain the number 4 and n will contain the number 3. A vector is a matrix for which either m or n is equal to 1. If m is equal to 1, the matrix is a row vector; if n is equal to 1, the matrix is a column vector. Matrices and vectors may contain complex numbers. For example, the statement

≫ A = [1 2;3 4]+j*[5 6;7 8]

and the statement

≫ A = [1+5*j 2+6*j;3+7*j 4+8*j]

are equivalent, and they both produce the matrix

A =
   1.0000+5.0000i    2.0000+6.0000i
   3.0000+7.0000i    4.0000+8.0000i

Note that blanks must be avoided in the second expression for A. Try typing

≫A = [1 + 5*j 2 + 6*j 2 + 6*j;3 +7*j 4 + 8*j]

What is the size of A now?

MATLAB has several built-in functions to manipulate matrices. The special character, ', for prime denotes the transpose of a matrix. The statement A = [ 1 2 3;4 5 6;7 8 9]' produces the matrix

A = 
   1 4 7 
   2 5 8
   3 6 9

The rows of A' are the column of A, and vice versa. If A is a complex matrix, then A is its complex conjugate transpose or hermitian transpose. For an “unconjugate” transpose, use the two-character operator dot-prime (. '). Matrix and vector variables can be added, subtracted, and multiplied as regular variables if the sizes match. Only matrices of the same size can be added or subtracted. There is, however, an easy way to add or subtract a common scalar from each element of a matrix. For example, x = [1 2 3 4],x = x-1 produces the output

x = 
   1 2 3 4

x = 
   0 1 2 3

As discussed in the chapter on linear algebra, multiplication of two matrices is only valid if the inner sizes of the matrices are equal. In other words, A*B is valid if the second size of A (number of columns) is the same as the first size of B (number of rows). Let ai,jai,j represent the element of A in the ithith row and the jthjth column. Then the matrix A*B consists of elements

( A B ) i , j = k = 1 n a i , k b k , j ( A B ) i , j = k = 1 n a i , k b k , j (1)

where n n is the number of columns of A and the number of rows of B. Try typing A = [1 2 3;4 5 6];B = [7;8;9]; A*B. You should get the result

ans =
    50
    112

The inner product between two column vectors x and y is the scalar defined as the product x'*y or equivalently as y'*x For example, x = [1;2],y = [3;4],x'*y, leads to the result

ans =
    11

Similarly, for row vectors the inner product is defined as x*y'. The Euclidean norm of a vector is defined as the square root of the inner product between a vector and itself. Try to compute the norm of the vector [1 2 3 4]. You should get 5.4772. The outer product of two column (row) vectors is the matrix x*y' (x'*y).

Any scalar can multiply or be multiplied by a matrix. The multiplication is then performed element by element. Try A = [1 2 3;4 5 6;7 8 9];A*2. You should get

ans =
    2  4  6
    8 10 12
   14 26 28

Verify that 2*A gives the same result.

The inverse of a matrix is computed by using the function inv(A) and is only valid if A is square. If the matrix is singular, meaning that it has no inverse, a message will appear. Try typing inv(A). You should get

Warning: Matrix is close to singular or badly scaled.
   Results may be inaccurate. RCOND=2.937385e-18

ans =
     1.0e+16*
     0.3152  -0.6304   0.3152
    -0.6304   1.2609  -0.6304
     0.3152  -0.6304   0.3152  

The inverse of a matrix may be used to solve a linear system of equations. For example, to solve the system

you could type A = [1 2 3;1 -2 4;0 -2 1]; b = [2;7;3]; inv(A)*b and get

ans =
    1
   -1
    1
1 2 3 1 - 2 4 0 - 2 1 x 1 x 2 x 3 = 2 7 3 , 1 2 3 1 - 2 4 0 - 2 1 x 1 x 2 x 3 = 2 7 3 , (2)

Check to see that this is the correct answer by typing A*[1;-1;1]. What do you see?

MATLAB offers another way to solve linear systems, based on Gauss elimination, that is faster than computing the inverse. The syntax is A\b and is valid whenever A has the same number of rows as b. Try it.

The “Dot” Operator. Sometimes you may want to perform an operation element by element. In MATLAB, these element-by-element operations are called array operations. Of course, matrix addition and subtraction are already element-by-element operations. The operation A. *B denotes the multiplication, element by element, of the matrices A and B. Make two 3 ×3×3 matrices, A and B, and try

≫ A*B
≫ A.*B

Suppose we want to find the square of each number in A. The proper way to specify this calculation is

≫ A_squared=A.^2

where the period (dot) indicates an “array operation” to be performed on each element of A. Without the dot, A is multiplied by A according to the rules of matrix multiplication described in Chapter 4, giving a totally different result:

≫ A^2

Subtleties. Because MATLAB can do so many different mathematical functions with just a few keystrokes, there are times when a very slight change in what you type will lead to a different result. Using the matrix A entered earlier, type the following two lines:

≫ 2.^A
≫ 2 .^A                        %with a space after the 2.

In the first case, the dot is “absorbed” by the 2 as a decimal point and the ^ is taken as a matrix exponential. But, when the dot is separated from the 2 by a space, it becomes part of the operator (.^)(.^) and specifies that 2 should be raised to the power of each element in A. The point is, you should be very careful to type what you mean in an unambiguous way until you are familiar enough with MATLAB to know how the subtle situations wiIl be interpreted. An unambiguous way of typing the preceding lines is

≫ (2.)^A                     %for matrix exponential
≫ (2).^A                     %for array exponential.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks