Skip to content Skip to navigation

Connexions

You are here: Home » Content » Using MATLAB

Navigation

Recently Viewed

This feature requires Javascript to be enabled.

Using MATLAB

Module by: Anders Gjendemsjø. E-mail the author

User rating (How does the rating system work?)
Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

:
(0 ratings)

Summary: This module covers basic use of MATLAB, and you will be up and running on variables, matrices, mathematical operations and the built-in help system in just a few minutes.

Note: Your browser may not currently support MathML. See our browser support page for additional details. You can always view the correct math in the PDF version.

Matlab Help

MATLAB has a great on-line help system accessible using the help command. Typing

help <function>

will return text information about the chosen function. For example to get information about the built-in function sum type:

help sum

To list the contents of a toolbox type help <toolbox>, e.g. to show all the functions of the signal processing toolbox enter

help signal processing

If you don't know the name of the function but a suitable keyword use the lookfor followed by a keyword string, e.g.

lookfor 'discrete fourier'

To explore the extensive help system use the "Help menu" or try the commands helpdesk or demo.

Matrices, vectors and scalars

MATLAB uses matrices as the basic variable type. Scalars and vectors are special cases of matrices having size 1x1, 1xN or Nx1. In MATLAB, there are a few conventions for entering data:

  • Elements of a row are separated with blanks or commas.
  • Each row is ended by a semicolon, ;.
  • A list of elements must be surrounded by square brackets, [ ]

Example 1

It is easy to create basic variables.

x = 1 (scalar)

y = [2 4 6 8 10] (row vector)

z = [2; 4; 6; 8; 10] (column vector)

A = [4 3 2 1 0; 1 3 5 7 9] (2 x 5 matrix)

Regularly spaced values of a vector can be entered using the following compact notation

start:skip:end

Example 2

A more compact way of entering variables than in Example 1 is shown here:

y= 2 : 2 : 10

A=[4:-1:0;1:2:9]

If the skip is omitted it will be set to 1, i.e., the following are equivalent

start:1:end and start:end

To create a string use the single quotation mark " ' ", e.g. by entering x = 'This is a string'.

Indexing matrices and vectors

Indexing variables is straightforward. Given a matrix M the element in the i'th row, j'th column is given by M(i,j). For a vector v the i'th element is given by v(i). Note that the lowest allowed index in MATLAB is 1. This is in contrast with many other programming languages (e.g. JAVA and C), as well as the common notation used in signal processing, where indexing starts at 0. The colon operator is also of great help when accessing specific parts of matrices and vectors, as shown below.

Example 3

This example shows the use of the colon operator for indexing matrices and vectors.

A(1,:) returns the first row of the matrix A.

A(:,3) returns the third column of the matrix A.

A(2,1:5) returns the first five elements of the second row.

x(1:2:10) returns the first five odd-indexed elements of the vector x.

Basic operations

MATLAB has built-in functions for a number of arithmetic operations and functions. Most of them are straightforward to use. The Table below lists the some commonly used functions. Let x and y be scalars, M and N matrices.

Table 1: Common mathematical operations in MATLAB
  MATLAB
xyxy x*y
xyxy x^y
exex exp(x)
log(xx) log10(x)
ln(xx) log(x)
log2(xx) log2(x)
MNMN M*N
M-1M-1 inv(M)
MTMT M'
det(MM) det(M)

  • Dimensions - MATLAB functions length and size are used to find the dimensions of vectors and matrices, respectively.
  • Elementwise operations - If an arithmetic operation should be done on each component in a vector (or matrix), rather than on the vector (matrix) itself, then the operator should be preceded by ".", e.g .*, .^ and ./.

Example 4

Elementwise operations, part I

Let A= 1 1 1 1 A 1 1 1 1 . Then A^2 will return AA= 2 2 2 2 AA 2 2 2 2 , while A.^2 will return 12121212= 1 1 1 1 12 12 12 12 1 1 1 1 .

Example 5

Elementwise operations, part II

Given a vector x, and a vector y having elements yn=1sinxn yn 1 xn . This can be easily be done in MATLAB by typing y=1./sin(x) Note that using / in place of ./ would result in the (common) error Matrix dimensions must agree.

Complex numbers

MATLAB has excellent support for complex numbers with several built-in functions available. The imaginary unit is denoted by i or (as preferred in electrical engineering) j. To create complex variables z1=7+ z1 7 and z2=2eπ z2 2 e simply enter z1 = 7 + j and z2 = 2*exp(j*pi)

The Table below gives an overview of the basic functions for manipulating complex numbers, where zz is a complex number.

Table 2: Manipulating complex numbers in MATLAB
  MATLAB
Re(zz) real(z)
Im(zz) imag(z)
|z|z abs(z)
Angle(zz) angle(z)
z*z* conj(z)

Other Useful Details

  • A semicolon added at the end of a line tells MATLAB to suppress the command output to the display.
  • MATLAB and case sensitivity. For variables MATLAB is case sensitive, i.e., b and B are different. For functions it is case insensitive, i.e., sum and SUM refer to the same function.
  • Often it is useful to split a statement over multiple lines. To split a statement across multiple lines, enter three periods "..." at the end of the line to indicate that it continues on the next line.

Example 6

Splitting y=a+b+c y a b c over multiple lines.

y = a...
+ b...
+ c;

Content actions

Give Feedback:

E-mail the module author | Rate module ( How does the rating system work?)

Rating system

Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

(0 ratings)

Download:

Add module to:

My Favorites (?)

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

| A lens (?)

Definition of a lens

Lenses

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

What is in a lens?

Lens makers point to Connexions 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 Connexions 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