I.2 Communicating with MATLAB

I.2.1 Expressions and Variables

MATLAB provides mathematical expressions, which involve entire matrices. The building blocks of mathematical expressions in MATLAB are:

  • Variables
  • Numbers
  • Operators
  • Functions

Variables

Variables in MATLAB do not require any type of declarations or dimension statements. Variable names are not limited in a number of characters. A MATLAB variable name consists of a letter, followed by any number of letters, digits, or underscore. MATLAB uses only the first 31 characters of a variable name and ignores the rest. MATLAB is case sensitive, it distinguishes between lowercase and uppercase letters. X and x, for example, are not the same and represent two separate MATLAB variables.

Numbers

MATLAB uses conventional decimal notation. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. For example x=2+3i or x=2+3j represent the same imaginary numbers.

Operators

Expressions in MATLAB use familiar arithmetic operators:

+Plus

Minus

*Multiplication

^Power

/Division

‘Complex conjugate or transpose of matrix

( )Specify evaluation order

[ ]Specify vector and matrices

Functions

MATLAB provides a large number of standard elementary mathematical functions. The most commonly used functions are:

absAbsolute value

acosInverse cosine

acoshInverse hyperbolic cosine

anglePhase angle

asinInverse sine

asinhInverse hyperbolic sine

atanInverse tangent

atan2Four quadrant inverse tangent

atanhInverse hyperbolic tangent

ceilRound towards plus infinity

conjComplex conjugate

cosCosine

coshHyperbolic cosine

expExponential

fixRound towards zero

floorRound towards minus infinity

imagComplex imaginary part

logNatural logarithm

log10Base 10 (common) logarithm

realComplex real part

remRemainder after division

roundRound towards nearest integer

signSignum function

sinSine

sinhHyperbolic sine

sqrtSquare root

tanTangent

tanhHyperbolic tangent

In addition, several special functions provide values of useful constants. The default values for the following variables are:

pi3.14159265358979

i Imaginary unit i2 = -1

jThe same as i

epsA small number equal to 2-52

realminSmallest floating-point number, 2-1022

realmaxLargets floating-point number, 21023

infInfinity

nanNot-a-number

The variable names for the above functions are not reserved by MATLAB. It is possible to overwrite any of them with a new variable, such as

pi = 3.1416,

and then uses that value in subsequent calculations. The default value can be restored with

clear pi.

I.2.2Data Format

MATLAB stores all numbers using long format specified by the IEEE floating-point standard. Floating-point numbers have a finite precision of roughly significant digits and a finite range of roughly 10-308 to 10308 .

The format command in MATLAB controls only the numeric format of the data being displayed on the screen.

The format command does not affect the way MATLAB stores and manipulates the data. The following examples illustrate the effect of different formats on displaying the output.

Example I.2.1.1:

First we assign the value to a variable x, then we display x using different formats.

» x = 4/3

x =

1.3333

»

format short displays data in 5-digit scaled fixed point.

» format short

» x

x =

1.3333

»

format long displays data in 15-digit scaled fixed point.

» format long

» x

x =

1.33333333333333

»

format short e: displays data in 5-digit floating point.

» format short e

» x

x =

1.3333e+000

»

format long e: displays data in 16-digit floating point.

» format long e

» x

x =

1.333333333333333e+000

»

format hex: displays data in hexadecimal.

» format hex

» x

x =

3ff5555555555555

»

format bank: displays data in dollars and cents format.

» format bank

» x

x =

1.33

»

format rat: displays data using a ratio of small integers.

» format rat

» x

x =

4/3

»

format +: displays data using + for positive and - for negative numbers.

» format +

» x

x =

+

»

format by itself with no other entries will return to default (short) format.

» format

» x

x =

1.3333

»

Summary of formats

short5-digit scaled fixed points.

long15-digit scaled fixed points.

short e5-digit floating point.

long e16-digit floating point.

hexHexadecimal.

bankFixed dollars and cents.

ratRatio of small integers.

+Positive/negative designation.

I.2.3Input / Output Data

You can enter data into MATLAB in two different ways: directly from keyboard or reading from a data file.

Entering Data from Keyboard

The prompt “>”in the Command Window indicates that MATLAB is ready to accept input data or a command from the keyboard.

You can enter data from the keyboard in a simple step. For example,

» A=3.25

A =

3.2500

»

At this stage MATLAB has assigned value 3.25 to the variable A.

This value remains in a memory location reserved for A as long as you do not overwrite it or clear it, or by ending the current MATLAB session through termination of the

Command Window.

You can change the value of A by simply entering a new value for A. For example,

» A=2

A =

2

»

To clear the variable A, at the prompt type clear A.

» clear A

»

Note that if you typed clear instead of clear A, MATLAB would have cleared all variables without giving you a notice. So be very careful. This can be very painful when you have entered many variables and suddenly you have accidentally cleared them all!

Practice I.2.3.1

Assign values 2, 3, and 4 to variables A, B, and C, respectively.

There are different ways of entering these data from the keyboard. We mention two of them here.

Method 1: All in one line.

At the prompt, type A=2,B=3,C=4 and press Return or Enter. MATLAB responds

» A=2,B=3,C=4

A =

2

B =

3

C =

4

»

Method 2: Entering one at a time.

» A=2

A =

2

» B=2

B =

2

» C=3

C =

3

»

I.2.4Suppressing Output

A semicolon at the end of a MATLAB statement

prevents

MATLAB from displaying the output

Anytime you press the return or enter key, MATLAB takes two actions: first it automatically displays the input data, then it performs the necessary computation (if needed) and displays the output.

However, if we end the input statement with a semicolon (;), MATLAB does not display the input data but it performs the computation and displays the output. This is particularly useful when you generate a large array vector.

For example:

» a=0:0.001:1000;

»

In this example, you generated a vector containing 1000001 numbers. Imagine that if you had not ended the statement with a semicolon. Without the semicolon, MATLAB displays all numbers on your computer screen.

I.2.5Long Command Lines

MATLAB allows 4096 characters per line. However, it is impossible to display all of these on one line. If your statement is long and does not fit on one line, use three periods, . . . , followed by a return or enter to indicate that the statement continues on the next line. For example,

» x=2 + 1 - 5 +6 + 7 + 8 + 9 + 11 + ...

13+14+15-12 - 13 - 14

x =

42

»

I.2.6Entering and Manipulating Arrays and Matrices

You can enter arrays and matrices into MATLAB in several different ways.

  • Enter an explicit list of elements.
  • Load from an external data file
  • Generate arrays or matrices using built-in functions
  • Create arrays or matrices with your own functions in M-files. (M-files are explained in Chapter III)

Entering arrays

Arrays are entered into MATLAB using the following format:

v= initial value : difference between two consecutive elements : final value

The default value for the difference between two consecutive elements is 1.

For example:

» v=1:10

v =

1 2 3 4 5 6 7 8 9 10

»

You generated a row vector of length 10 consisting of the integers 1 through 10.

» v=10:0.1:11

v =

Columns 1 through 7

10.0000 10.1000 10.2000 10.3000 10.4000 10.5000 10.6000

Columns 8 through 11

10.7000 10.8000 10.9000 11.0000

».

In the above example, you generated a vector containing numbers from 10 through 11 with an increment of 0.1.

You can access an individual or a range of elements from a vector using the following format:

v(from index : to index)

Example: I.2.6.1:

Enter a vector v containing elements 1 through 10; do not display the elements. Add elements 1 through 5 to elements 6 through 10 of this vector v.

The elements 1 through 5 are: 1, 2, 3, 4, 5 and the elements 6 through 10 are: 6, 7, 8, 9, 10. We would like to add 1 to 6, 2 to 7, … etc.

» v=1:10;

» v(1:5)+v(6:10)

ans =

7 9 11 13 15

»

Matrices are entered into MATLAB using the following two format:

M= [ first row; second row;…; last row] or

M= [first row

2nd row

.

.

.

last row ]

For example to enter a 3x3 matrix you can enter either

» M=[1 2 3; 4 5 6; 7 8 9]

M =

1 2 3

4 5 6

7 8 9

»

or

» M=[1 2 3

4 5 6

7 8 9]

M =

1 2 3

4 5 6

7 8 9

»

To access different blocks of matrices you can use the following format:

M1 = M (from row: to row , from column: to column )

where M is the original matrix and M1 is the desired sub-matrix.

Example: I.2.6.2:

a) Enter a 3x3 matrix M with the first row being 1, 2, and 3, the second row being 4, 5, and 6, and the third row being 7, 8, and 9, respectively. Do not display the matrix.

b) Assign the elements from row 1 through 2 and columns 2 through 3 to a matrix M1.

c) Assign the elements from row 2 through 3 and columns 1 through 2 to a matrix M2.

d) Obtain a matrix MSUM containing the sum of the corresponding elements in M1 and M2.

a)The matrix M is:

b) M1 is:

c) M2 is

d) MSUM is

You can do this in MATLAB as shown below.

» M=[1 2 3; 4 5 6; 7 8 9];

» M1=M(1:2,2:3)

M1 =

2 3

5 6

» M2=M(2:3,1:2)

M2 =

4 5

7 8

» MSUM=M1+M2

MSUM =

6 8

12 14

»

I.2.7Generating a Matrix from Arrays

You can combine vectors to generate a matrix. If vectors v1, v2, v3, and v4 are row vectors of dimension 1x4, then the matrix M can be formed using these vectors.

M=[v1; v2; v3; v4]

Example: I.2.7.1:

a) Generate row vectors v1, v2, v3, and v4 containing element, 1,2,3,4, and 5,6,7,8, and 9, 10, 11, 12, and 13, 14, 15, 16, respectively.

b) Generate matrix M containing v1, v2, v3, and v4 as its 1st, 2nd, 3rd, and 4th rows.

» v1=1:4

v1 =

1 2 3 4

» v2=5:8

v2 =

5 6 7 8

» v3=9:12

v3 =

9 10 11 12

» v4=13:16

v4 =

13 14 15 16

» M=[v1; v2; v3; v4]

M =

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

»

I.2.8Special Matrices

Identity Matrix

A n x n identity matrix is a matrix whose diagonal elements are ones and all other elements are zeros.

» eye(4)

ans =

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

»

Note that you can generate an m x n matrix whose diagonal elements are ones and zeros elsewhere using the eye function.

» eye(3,4)

ans =

1 0 0 0

0 1 0 0

0 0 1 0

»

Companion matrix

An nth order polynomial is represented by

.

You can represent this polynomial in MATLAB as a vector with entries being the coefficients Once you enter this polynomial as a vector in MATLAB, you can solve for its roots. For example, you can solve for the polynomial

as

» p=[1 -5 6];

» r=roots(p)

r =

3

2

»

The vector r holds the roots of the polynomial p(x).

The companion matrix of p is a matrix whose eigenvalues are the roots of the polynomial p. For example, the companion matrix corresponding to the polynomial

is:

» p=[1 -5 6];

» A=compan(p)

A =

5 -6

1 0

»

In other words, if we compute the eigenvalues of A, they will be identical to the roots of the polynomial p.

» eig(A)

ans =

3

2

»

Hadamard matrices

The Hadamard transform has many applications in signal and image processing. The 1-D and 2-D Hadamard transformations are given in the following equations:

1-D Hadamard transform

2-D Hadamard transform

The elements of the Hadamard matrix H are either +1 or -1. You can generate an nxn Hadamard matrix by

The 4x4 Hadamard matrix is:

» A=hadamard(4)

A =

1 1 1 1

1 -1 1 -1

1 1 -1 -1

1 -1 -1 1

»

Note that the rows and columns of the Hadamard matrices are orthogonal, that is if we sum the product of any two rows or any two columns of a Hadamard matrix the result is zero. Also, if we multiply the transpose of a Hadamard matrix by a Hadamard matrix itself, the product is equal to n times an identity matrix. Which states that the inverse of a Hadamard matrix is equal to its transpose divided by n. Lets try this on a 4x4 Hadamard matrix.

» A=hadamard(4);

» trans_a=A';

» trans_a*A

ans =

4 0 0 0

0 4 0 0

0 0 4 0

0 0 0 4

» trans_a*A/4

ans =

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

»

In addition to the above matrices, MATLAB can generate many more special matrices. Examples are:

FunctionPurpose

hilbHilbert matrix

Inverse Hilbert transform invhilb

magicMagic square

pascalPascal matrix

rosserClassic symmetric eigenvalue test problem

toeplitzToeplitz matrix

vanderVandermonde matrix

wilkinsonWilkinson's eigenvalue test matrix

1