Professional Documents
Culture Documents
)
A string is an array of characters
Strings have many uses in MATLAB
Display text output
Specify formatting for plots
Input arguments for some functions
Text input from user or data files
(Creating Strings)
We create a string by typing characters within single
quotes (').
Many programming languages use the quotation
'"Greg''s car"'
Examples:
'MATLAB'
2
In a string variable
Numbers are stored as an array
A one-line string is a row vector
Number of elements in vector is number of
characters in string
>> name = 'Howard the Duck';
>> size( name )
ans =
1 15
4
Example:
>> word = 'dale';
>> word(1)
ans = d
>> word(1) = 'v'
word = vale
>> word(end) = []
word = val
>> word(end+1:end+3) = 'ley'
word = valley
Problem
4
characters
'Greg';
>> names = [
using ==> vertcat
3
characters
'Jon'
]???
Error
Extra space
(String Padding)
Making sure each line of text has the same number of characters is a big pain.
MATLAB solves problem with char function, which pads each line on the right
with enough spaces so that all lines have the same number of characters.
>> question=char('Romeo, Romeo,','Wherefore art thou', 'Romeo?')
question =
Romeo, Romeo,
Wherefore art thou
Romeo?
>> size (question)
ans =
3 18
10
R o m e o ,
W h e r
e f
R o m e o ,
o r
a r
h o u
R o m e o ?
Array Operators
A.* B
multiplies each element in array A times the
corresponding element in array B
A./B
divides each element in array A by the corresponding
element in array B
A.^B
raises each element in array A to the power in the
corresponding element of array B
array.
Use to subtract one array from another or to
13
Array Multiplication
There are two ways of multiplying matrices
matrix multiplication and elementwise
multiplication.
MATRIX MULTIPLICATION
The type used in linear algebra.
MATLAB denotes this with an asterisk (*).
The number of columns in the left matrix
Matrix Multiplication
18
Matrix Multiplication
19
>> A = randi(3,3)
A =
3
>> B=randi(3,3)
B =
3
>> AB = A*B
AB =
15
18
12
17
19
13
13
14
12
>> BA = B*A
BA =
19
16
12
11
11
21
18
18
>> AB == BA
ans =
0
20
Vector Multiplication
When performing matrix multiplication on
two vectors:
They must both be the same size.
One must be a row vector and the other a
column vector.
If the row vector is on the left, the product
is a scalar.
If the row vector is on the right, the product
21
>> v = [ -1 0 1 ]'
v =
-1
0
1
>> h * v
ans =
4
>> v * h
ans =
-2
0
2
-4
0
4
-6
0
6
22
size.
or horizontal vectors.
Result is always a scalar.
EXAMPLE
>> h = [ 2 4 6 ]
h =
2
4
6
>> v = [ -1 0 1 ]'
v =
-1
0
1
>> dot(h,v)
ans =
4
>> dot(v,h)
ans =
4
23
Description
24
Cross Product
cross(a,b) computes the
More info:
http://en.wikipedia.org/wiki/Cros
s_product
EXAMPLE
>> a = [ 1 2 0 ]
a =
1 2 0
>> b = [3 4 0 ]
b =
3 4 0
>> cross(a,b)
ans =
0 0 -2
25
Description
26
Identity Matrix
A square matrix with ones on main diagonal
and zeros elsewhere.
When we do a matrix multiplication on any
identity matrix
eye(n) makes an nn
27
Determinants
A determinant is a function associated
with square matrices
In math, determinant of A is written as
det(A) or |A|
In MATLAB, compute determinant of A
with det(A)
A matrix has an inverse only if it is
Determinants Example
with Cross Product
29
inv(A)
30
Left division, \:
Left division is one of MATLAB's two kinds of array
division
Used to solve the matrix equation AX=B
A is a square matrix, X, B are column vectors
Solution is
X = A-1B
31
results
32
Right division, /:
Right division is the other kind of MATLAB's
array division
Used to solve the matrix equation
XC=D
X = DC-1
33
34
Array Division
35
always elementwise.
Multiplication, division, exponentiation
37
38
ELEMENTWISE MULTIPLICATION
Use .* to get elementwise multiplication (notice
3 -2
40
42
EXAMPLE
>> A = [1 2; 3 4];
>> B = [0 1/2; 1 -1/2];
>> A .* B
>> ans
0
3 -2
>> A * B
ans =
2.0000
-0.5000
4.0000
-0.5000
43
cos(x) is
44
order
45
median(v) median
std(v) standard deviation
dot(v,w) dot (inner product); v, w both
47