You are on page 1of 21

# Chapter 2 contd.

MATLAB

>> f = 'Miles ';
>> s = 'Davis'

>> x = [f s]
x =
Miles Davis

## Ellipsis (...): Used to continue long lines

>> a = [1 2 3 4 5 ...
6 7 8]
a =
1
2
3
4
5

You cannot use an ellipsis within single quotes to continue a string. But you can
piece together shorter strings with ellipsis
>> quote = ['Any fool can make a rule,' ...
' and any fool will mind it']
quote =
Any fool can make a rule, and any fool will mind it

Initializing Variables

## Explicitly list the values, or

Read from a data file, or
Use the colon (:) operator, or
Read from the keyboard
A = [1; 3; 5; 10];
C = [2 3 5 1; 0 1

B = [1 3 5; -6 4 -1]
(continuation)

1 -2; 3 5 1 -3]
E = [A; 1; A];

F = [C(2,3); A]

Colon Operator

## Creating new matrices from an existing matrix

C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]
F = C(:, 2:3) (=> F = [2,5; 0,1; 2,-1; 1,4])
1
1
C
3

0
1
2 1

1
4
2

5
2
0

F
2 1

4
1

Colon Operator

## Creating new matrices from an existing matrix

C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]
E = C(2:3,:)
1
1
C
3

0
1
2 1

1
4

1
1 0
E

3 2 1

Colon Operator

G = C(3:4,1:2)
1
1
C
3

0
1
2 1

1
4

3 2
G

0
1

## Array Creation - Colon Operator

The colon operator : is useful in several contexts. It
can be used to create a linearly spaced array of
points using the notation
start:diffval:limit
where start is the first value in the array, diffval is the
difference between successive values in the array, and
limit is the boundary for the last value (though not
necessarily the last value).
>>1:0.6:3
ans =
1.0000

1.6000

2.2000

2.8000

Colon Operator

Variable_name = a:step:b

>> x = 0.0:0.5:2.5

## x = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]

>> x = 0.0:0.5:2.4

## x = [0.0, 0.5, 1.0, 1.5, 2.0]

Negative increment

>> v = 10:-1:2

v = [10, 9, 8, 7, 6, 5, 4, 3, 2]

## Colon Operator - Notes

If diffval is omitted, the default value is 1:
>>3:6
ans =
3
4
5
6
To create a decreasing series, diffval must be negative:
>> 5:-1.2:2
ans =
5.0000
3.8000
2.6000
If start+diffval>limit for an increasing series or
start+diffval<limit for a decreasing series, an empty
matrix is returned:
>>5:2
ans =
Empty matrix: 1-by-0
To create a column, transpose the output of the colon
operator, not the limit value; that is, (3:6) not 3:6

linspace Function

## linspace(x1, x2) gives 100 evenly spaced values

between x1 and x2
x = linspace(x1, x2)
linspace(a, b, n) generate n equally spaced points
between a and b
x = linspace(a, b, n)

linspace(0,2,11)
ans =

Columns 1 through 7
0

0.2000

0.4000

0.6000

1.8000

2.0000

Columns 8 through 11
1.4000

1.6000

0.8000

1.0000

1.2000

logspace Function

## logspace(a, b, n) generates a logarithmically

equally spaced row vector
x = logspace(a,b,n)
logspace(a, b) generates 50 logarithmically
equally spaced points
x = logspace(a,b)

logspace(-4,2,7)
ans =
0.0001

0.0010

0.0100

0.1000

1.0000

10.0000

100.0000

## Array Creation - Built In

There are several built-in functions to create
arrays:
zeros(r,c) will create an r row by c column
matrix of zeros
zeros(n) will create an n by n matrix of zeros
ones(r,c) will create an r row by c column
matrix of ones
ones(n) will create an n by n matrix one ones
eye(n) will create an n by n identity matrix

## help elmat has, among other things, a list

of the elementary matrices

Special Matrices
1 0 0

eye( 3) 0 1 0
0 0 1

0 0

zeros(3,2) 0 0
0 0

1 1 1

ones( 3) 1 1 1
1 1 1

1 1 1 1
ones(2,4)

1 1 1 1

## Scalar Arithmetic Operations

In order of priority
S ymbol
Operation
MATLAB Form
^
Exponentiation a b
a^ b

Negation a
a
* /
Multiplication and division ab; a b a * b; a / b
b
\
Left division a \ b (Matrix inverse) a \ b
a

## Addition and subtraction a b

a b; a b

Example: x = (a + b*c)/d^2
count = count + 1

Order of Precedence of
Arithmetic Operations
The priority order can be overridden with parentheses
a=3; b=5; c=2;

y = -7.3^2

s1 = a-b*c

y =

s1 =
-7
s2=(a-b)*c
s2 =

-53.2900
y=(-7.3)^2
y =

53.2900

-4

## Multiplication has higher

priority than subtraction

## Exponentiation has higher

priority than negation

Array Operations

## An array operation is performed

element-by-element
MATLAB: C = A.*B;
C(1) A(1) * B(1);
C(2) A(2) * B(2);
C(3) A(3) * B(3);
C(4) A(4) * B(4);
C(5) A(5) * B(5);

Element-by-Element Operations
Symbol Operation

Scalar-array addition

Form
Ab

Example
[4, 6] 3 [7, 9]

.*
./
.\
.^

Ab
A B
A B
A. * B
A. / B
A. \ B
A. ^ B

[8,
[4,
[4,
[3,
[3,
[3,
[4,

Scalar-array subtraction
Array addition
Array subtraction
Array multiplication
Array right division
Array left division
Array exponentiation

3] 6 [2, 3]
6] [8, 3] [12, 9]
6] [8, 3] [ 4, 3]
6]. *[2, 3] [6, 18]
7]. / [8, 5] [3 / 8, 7 / 5] [0.375,1.400]
7]. \ [8, 5] [3 \ 8, 7 \ 5] [2.667,0.7143]
2]. ^ 3 [4 ^ 3, 2 ^ 3] [64, 8]

## 3. ^ [2, 5] [3 ^ 2, 3 ^ 5] [9, 243]

[5, 3]. ^ [2, 4] [5 ^ 2, 3 ^ 4 ] [25, 81]

## Vector and Matrix operations

1
2
3

a 3
b 4
a b 7
5
6
11
But a*b gives an error (undefined) because
dimensions are incorrect. Need to use .*

1* 2 2

a .* b 3 * 4 12
5 * 6 30

## Vectorized Matrix Operations

A 2 3 8 1
B 1 4 5

C A .* B 2 12 40 2
D A . / B 2 0.75 1.6 0.5
E A .^ 3 8 27

512 1

F ( 3 ).^ B 3 81 243 9

## Array Operations for

m n Matrices
2
3
4
1
A 1 : 4; - 1 : -1 : -4; 3 1 2 - 1 1 2 3 4
3
1
2 1
10
15
20
5

B A.* 5 5 10 15 20
15
5
10
5

8
27
64
1

C A.^ 3 1 8 27 64
27
1
8
1

Matrix Transpose
x 4 2 3 ;
4

x' 2
3

y 3 1 2

;
y' 1
- 2
4 8
4
12
x'* y 2 3 1 2 6 2
4
3
9
3 6
3

x * y' 4 2 3 1 ( 4 )( 3 ) ( 2 )( 1 ) 3( 2 ) 4
- 2