Anda di halaman 1dari 54

CSE123

Lecture 4
Arrays,Matrices and Array
Operations
Definitions
Scalars: Variables that represent single numbers. Note that
complex numbers are also scalars, even though they have two
components.

Arrays: Variables that represent more than one number. Each
number is called an element of the array. Array operations
allow operating on multiple numbers at once.

Row and Column Arrays (Vector): A row of numbers (called a
row vector) or a column of numbers(called a column vector).

Two-Dimensional Arrays (Matrix): A two-dimensional table of
numbers, called a matrix.
Vector Creation by Explicit List
A vector in Matlab can be created by an explicit list, starting with a left
bracket, entering the values separated by spaces (or commas) and
closing the vector with a right bracket.
>>p = [3,7,9]
p =
3 7 9

You can create a column vector by using the transpose
notation (').

>>p = [3,7,9]'
p =
3
7
9
Vector Creation by Explicit List
You can also create a column vector by separating the
elements by semicolons. For example,

>>g = [3;7;9]
g =
3
7
9
Vector Creation by Explicit List
>>x=[0 .1*pi .2*pi .3*pi .4*pi .5*pi .6*pi .7*pi .8*pi .9*pi pi]
>>y=sin(x)
>>y =
Columns 1 through 7
0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511
Columns 8 through 11
0.8090 0.5878 0.3090 0.0000
Vector Addressing / indexation
A vector element is addressed in Matlab with an integer index (also
called a subscript) enclosed in parentheses.
>> x(3)
ans =
0.6283
>> y(5)
ans =
0.9511
Colon notation: Addresses a block of elements. The format is:
(start:increment:end)
Note start, increment and end must be positive integer numbers.

If the increment is to be 1, a shortened form of the notation may be used:
(start:end)
>> x(1:5)
ans =
0 0.3142 0.6283 0.9425 1.2566
>> x(7:end)
ans =
1.8850 2.1991 2.5133 2.8274 3.1416
>> y(3:-1:1)
ans =
0.5878 0.3090
>> y([8 2 9 1])
ans =
0.8090 0.3090 0.5878 0
Vector Creation Alternatives
Combining: A vector can also be defined using another vector that has
already been defined.
>> B = [1.5, 3.1];
>> S = [3.0 B]
S =
3.0000 1.5000 3.1000

Changing: Values can be changed by referencing a specific address
Extending: Additional values can be added using a reference to a specific
address.
>> S(2) = -1.0;
>> S
S =
3.0000 -1.0000 3.1000
>> S(4) = 5.5;
>> S
S =
3.0000 -1.0000 3.1000 5.5000
>> S(7) = 8.5;
>> S
S =
3.0000 -1.0000 3.1000 5.5000 0 0 8.5000
Vector Creation Alternatives
Colon notation:
(start:increment:end)
where start, increment, and end can now be floating point numbers.
x=(0:0.1:1)*pi
x =
Columns 1 through 7
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850
Columns 8 through 11
2.1991 2.5133 2.8274 3.1416

linspace: generates a vector of uniformly incremented values, but instead of
specifying the increment, the number of values desired is specified. The form:
linspace(start,end,number)
1

=
number
start end
increment
The increment is computed internally, having the value:
Vector Creation Alternatives
>> x=linspace(0,pi,11)
x =
Columns 1 through 7
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850
Columns 8 through 11
2.1991 2.5133 2.8274 3.1416
logspace(start_exponent,end_exponent,number)
To create a vector starting at 10
0
= 1,ending at 10
2
= 100 and having 11 values:
>> logspace(0,2,11)
ans =
Columns 1 through 7
1.0000 1.5849 2.5119 3.9811 6.3096 10.0000 15.8489
Columns 8 through 11
25.1189 39.8107 63.0957 100.0000
Length, and Absolute Value of a Vector

Keep in mind the precise meaning of these terms when
using MATLAB.

The length command gives the number of elements in
the vector.

The absolute value of a vector x is a vector whose
elements are the absolute values of the elements of x.
2-9
For example, if x = [2,-4,5],

- its length is 3; (computed from length(x))

- its absolute value is [2,4,5] (computed
from abs(x)).

2-10
Matrix arrays in Matlab
Vector
Use square brackets.
Separate elements on the same row with spaces or
commas.

&
Matrix



Use semi-colon to go to the next row.
A= [ 1 2 3 ];
C= [ 5 ; 6 ; 7 ];
( ) 3 2 1 A =
|
|
.
|

\
|
=
4 3
2 1
B
|
|
|
.
|

\
|
=
7
6
5
C
B= [ 1 2 ; 3 4 ];
A= [ 1, 2, 3 ];
Matrix Arrays
A matrix array is 2D, having both multiple rows and multiple columns.
Creation of 2D arrays follows that of row and column vectors:
Begin with [ end with ]
Spaces or commas are used to separate elements in a row.
A semicolon or Enter is used to separate rows.
>> h = [1 2 3
4 5 6
7 8 9]
h =
1 2 3
4 5 6
7 8 9

>> k = [1 2;3 4 5]
??? Number of elements in each
row must be the same.
>>f = [1 2 3; 4 5 6]
f =
1 2 3
4 5 6

>> g = f
g =
1 4
2 5
3 6

Manipulations and Combinations:
>> A=10*ones(2,2)
A = 10 10
10 10
Special matrix creation
>> B=10*rand(2,2)

Matrix full of 10:
Matrix of random
numbers between
0 and 10
B = 4.5647 8.2141
0.1850 4.4470
>> C= -rand(2,2)

Matrix of random
numbers between
-1 and 0
C = -0.4103 -0.0579
-0.8936 -0.3529
>> C=2*rand(2,2) ones(2,2)
>> C=2*rand(2,2) -1
Matrix of random
numbers between
-1 and 1
C = -0.6475 0.8709
-0.1886 0.8338
Concatenation: Combine two (or more) matrices into one
Special matrix creation
Notation:



C=[ A, B ]


Square
brackets
>> A=ones(2,2);
>> B=zeros(2,2);
>>C=[A , B]
>>D=[A ; B]
D = 1 1
1 1
0 0
0 0
C = 1 1 0 0
1 1 0 0
Obtain a single value from a matrix:

Ex:




want to know a
21


Matrix indexation
|
|
|
.
|

\
|
=
4 2 1
1 2 3
3 2 1
A
Notation:



A(2,1)



Row index Column index
>> A=[1 2 3; 3 2 1; 1 2 4];
>> A(2,1)
ans =
3
>> A(3,2)
ans =
2
Obtain more than one value from a matrix:

Ex: X=1:10






Matrix indexation
>> A=[1 2 3; 3 2 1; 1 2 4];
>> B=A(1:3,2:3)
B = 2 3
2 1
2 4
>> C=A(2,:)
C = 3 2 1
Colon
Colon defines a
range: 1 to 10
Notation:



A(1:3,2:3)



Row 1 to 3
Column 2 to 3
Colon can also be used as a wildcard
Row 2, ALL
columns
|
|
|
.
|

\
|
=
4 2 1
1 2 3
3 2 1
A
Matrix size
Command Description
s = size(A)
For an m x n matrix A, returns the two-element row
vector s = [m, n] containing the number of rows and
columns in the matrix.
[r,c] = size(A) [r,c] = size(A) Returns two scalars r and c
containing the number of rows and columns in A,
respectively.
r = size(A,1) Returns the number of rows in A in the variable r.
c = size(A,2) Returns the number of columns in A in the variable c.
Matrix size
>> A = [1 2 3; 4 5 6]
A =
1 2 3
4 5 6

>> s = size(A)
s =
2 3

>> [r,c] = size(A)
r =
2
c =
3
>> whos
Name Size Bytes Class
A 2x3 48 double array
ans 1x1 8 double array
c 1x1 8 double array
r 1x1 8 double array
s 1x2 16 double array

Additional Array Functions
max(A) Returns the algebraically largest element in A if
A is a vector.
Returns a row vector containing the largest
elements in each column if A is a matrix.
If any of the elements are complex, max(A)
returns the elements that have the largest
magnitudes.

2-18
Additional Array Functions
[x,k] =
max(A)

min(A)
and
[x,k] =
min(A)
Similar to max(A) but stores the maximum
values in the row vector x and their
indices in the row vector k.

Like max but returns minimum values.


2-19
2-20
sort(A) Sorts each column of the
array A in ascending order
and returns an array the
same size as A.
sum(A) Sums the elements in each
column of the array A and
returns a row vector
containing the sums.

Additional Array Functions (Table 2.11)
zeros(M,N)
Matrix of zeros


ones(M,N)
Matrix of ones


eye(M,N)
Matrix of ones on the
diagonal

rand(M,N)
Matrix of random
numbers between 0
and 1

>> A=zeros(2,3)

A = 0 0 0
0 0 0
>> B=ones(2,2)

B = 1 1
1 1
>> D=rand(3,2)

D = 0.9501 0.4860
0.2311 0.8913
0.6068 0.7621
Special matrix creation
>> C=eye(2,2)

C = 1 0
0 1
Operations on vectors and matrices in Matlab
Math Matlab
Addition/subtraction A+B
A-B
Multiplication/ division
(element by element)
A.*B
A./B
Multiplication
(Matrix Algebra)

A*B

Transpose: A
T
A
Inverse: A
-1
inv(A)
Determinant: |A| det(A)
single quote
Array Operations
Scalar-Array Mathematics
Addition, subtraction, multiplication, and division of an array by a scalar
simply apply the operation to all elements of the array.
>> f = [1 2 3; 4 5 6]
f =
1 2 3
4 5 6
>> g = 2*f -1
g =
1 3 5
7 9 11
Array Operations
Element-by-Element Array-Array Mathematics
When two arrays have the same dimensions, addition, subtraction,
multiplication, and division apply on an element-by-element basis.
Operation Algebraic Form Matlab
Addition a + b a + b
Subtraction a b a - b
Multiplication a x b a .* b
Division a / b a ./ b
Exponentiation a
b
a .^ b
|
|
|
.
|

\
|
=
33 32 31
23 22 21
13 12 11
a a a
a a a
a a a
A
|
|
|
.
|

\
|
=
33 32 31
23 22 21
13 12 11
b b b
b b b
b b b
B
|
|
|
.
|

\
|
=
33 33 32 32 31 31
23 23 22 22 21 21
13 13 12 12 11 11
b a b a b a
b a b a b a
b a b a b a
B A


MATRIX Addition (substraction)


M
N
M
N
M
N
Array Operations
|
|
|
.
|

\
|
=
9 8 7
6 5 4
3 2 1
A
|
|
|
|
|
.
|

\
|
= + B A
Examples: Addition & Subtraction
|
|
|
.
|

\
|
=
9 8 7
6 5 4
3 2 1
B
2 4 6
8
10
12
14 16 18
|
|
|
|
|
.
|

\
|
= B A
0 0 0
0
0 0
0 0 0
Array Operations
Array Operations
Element-by-Element Array-Array Mathematics
>> A = [2 5 6];
>> B = [2 3 5];
>> C = A.*B
C =
4 15 30

>> D = A./B
D =
1.0000 1.6667 1.2000

>> E = A.^B
E =
4 125 7776

>> F = 3.0.^A
F =
9 243 729
|
|
|
.
|

\
|
=
33 32 31
23 22 21
13 12 11
a a a
a a a
a a a
A
|
|
|
.
|

\
|
=
33 32 31
23 22 21
13 12 11
b b b
b b b
b b b
B
|
|
|
.
|

\
|



=
33 33 32 32 31 31
23 23 22 22 21 21
13 13 12 12 11 11
b a b a b a
b a b a b a
b a b a b a
B A
MATRIX Multiplication (element by element)
M
N
|
|
|
.
|

\
|
= B A
M
N
M
N
dot
multiply
NOTATION
Array Operations
|
|
|
.
|

\
|
=
9 8 7
6 5 4
3 2 1
A
|
|
|
|
|
.
|

\
|
= B A
Examples: Multiplication & Division
(element by element)
|
|
|
.
|

\
|
=
9 8 7
6 5 4
3 2 1
B
1 4 9
16
25 36
49 64 81
|
|
|
|
|
.
|

\
|
= B / A
1 1 1
1
1 1
1 1 1
Array Operations
The built-in MATLAB functions such as sqrt(x) and
exp(x) automatically operate on array arguments to
produce an array result the same size as the array
argument x.

Thus these functions are said to be vectorized functions.

For example, in the following session the result y has
the same size as the argument x.

>>x = [4, 16, 25];
>>y = sqrt(x)
y =
2 4 5
2-33
However, when multiplying or dividing these functions, or
when raising them to a power, you must use element-by-
element operations if the arguments are arrays.

For example, to compute z = (e
y
sin x) cos
2
x, you must type

z = exp(y).*sin(x).*(cos(x)).^2.

You will get an error message if the size of x is not the same
as the size of y. The result z will have the same size as x
and y.
2-34 More? See pages 67-69.
We can raise a scalar to an array power. For example, if
p = [2, 4, 5], then typing 3.^p produces the array
[3
2
, 3
4
, 3
5
] = [9, 81, 243].

Remember that .^ is a single symbol. The dot in 3.^p
is not a decimal point associated with the number 3. The
following operations, with the value of p given here, are
equivalent and give the correct answer:

3.^p
3.0.^p
3..^p
(3).^p
3.^[2,4,5]
2-38 More? See pages 70-72.
Array Operations
The matrix multiplication of m x n matrix A and nxp matrix B yields
m x p matrix C, denoted by
C = AB
Element c
ij
is the inner product of row i of A and column j of B

=
=
n
k
kj ik ij
b a c
1
Note that AB BA
Matrix Multiplication
|
|
|
.
|

\
|
=
33 32 31
23 22 21
13 12 11
a a a
a a a
a a a
A
|
|
|
.
|

\
|
=
33 32 31
23 22 21
13 12 11
b b b
b b b
b b b
B
|
|
|
.
|

\
|
+ + + + + +
+ + + + + +
+ + + +
=
33 33 23 32 13 31 32 33 22 32 12 31 31 33 21 32 11 31
33 23 23 22 13 21 32 21 22 22 12 21 31 23 21 22 11 21
33 13 23 12 13 11 32 13 22 12 12 11
b a b a b a b a b a b a b a b a b a
b a b a b a b a b a b a b a b a b a
b a b a b a b a b a b a
B A
Matrix Multiplication
Row 1
C
o
l
u
m
n

1

31 13 21 12 11 11
b a b a b a + +
|
|
|
.
|

\
|
= B A
multiply
NOTATION
M
1

N
1

M
2

N
2

M
1

N
2

N1=M2
Array Operations
|
|
|
.
|

\
|
=
2 1 3
1 2 3
3 2 1
A
|
|
|
|
|
.
|

\
|
= B A
Example: Matrix Multiplication
16 9 11
12
11 13
12 10 14
|
|
|
.
|

\
|
=
2 1 3
1 2 3
3 2 1
B
1x1 + 2x3 +3x3
1x2 + 2x2 +3x1
1x3 + 2x1 +3x2
Array Operations
Solving systems of linear equations

Example: 3 equations and 3 unknown

1x + 6y + 7z =0
2x + 5y + 8z =1
3x + 4y + 5z =2

Can be easily solved by hand, but what can we do if it we have 10 or 100
equations?

Array Operations
Solving systems of linear equations

First, write a matrix with all the (xyz)
coefficients
|
|
|
.
|

\
|
=
5 4 3
8 5 2
7 6 1
A
1x + 6y + 7z = 0
2x + 5y + 8z = 1
3x + 4y + 5z = 2
Write a matrix with all the constants
|
|
|
.
|

\
|
=
2
1
0
B
Finally, consider the matrix of unknowns
|
|
|
.
|

\
|
=
z
y
x
S
Array Operations
Solving systems of linear equations

A x S = B
A x S = B A
-1
x A
-1
x
(A
-1
x A) x S = A
-1
x B
I x S = A
-1
x B
S = A
-1
x B
Array Operations
Solving systems of linear equations

1x + 6y + 7z =0
2x + 5y + 8z =1
3x + 4y + 5z =2
The previous set of equations can be expressed in the following vector-matrix
form:
A x S = B
|
|
|
.
|

\
|
5 4 3
8 5 2
7 6 1
|
|
|
.
|

\
|
=
2
1
0
|
|
|
.
|

\
|
z
y
x
X

Array Operations





Matrix Determinant
Notation: Determinant of A = |A| or det(A)
The determinant of a square matrix is a very useful value for finding if a system
of equations has a solution or not.
If it is equal to zero, there is no solution.
det(M)= m
11
m
22
m
21
m
12

|
|
.
|

\
|
=
22 21
12 11
m m
m m
M
Formula for a 2x2 matrix:
IMPORTANT: the determinant of a matrix is a scalar
Array Operations





Matrix Inverse
Notation: inverse of A = A
-1

or inv(A)
The inverse of a matrix is really important concept, for matrix algebra
Calculating a matrix inverse is very tedious for matrices bigger than 2x2.
We will do that numerically with Matlab.
|
|
.
|

\
|
=
22 21
12 11
m m
m m
M
M
-1
=
Formula for a 2x2 matrix:
|
|
.
|

\
|


11 21
12 22
) det(
1
m m
m m
M
IMPORTANT: the inverse of a matrix is a matrix
Array Operations
Property of identity matrix:

I x A = A
and A x I = A

Matrices properties
Property of inverse :


A x A
-1
= I

and A
-1
x A = I

Example:





|
|
|
.
|

\
|
=
|
|
|
.
|

\
|

|
|
|
.
|

\
|
1 0 0
0 1 0
0 0 1
2 . 0 4 . 0 8 . 0
2 . 0 6 . 0 2 . 0
6 . 0 2 . 0 4 . 0
x
1 0 2
1 2 1
2 1 1
Array Operations
x + 6y + 7z =0
2x + 5y + 8z =1
3x + 4y + 5z =2 |
|
|
.
|

\
|
=
5 4 3
8 5 2
7 6 1
A
|
|
|
.
|

\
|
=
2
1
0
B
|
|
|
.
|

\
|
=
z
y
x
S
In Matlab:
>> A=[ 1 6 7; 2 5 8; 3 4 5]
>> B=[0;1;2];
>> S=inv(A)*B









Verification:

>> det(A)

ans =

28
Solving systems of equations in Matlab

>>
S =

0.8571
-0.1429
0
x + 6y + 7z =0
2x + 5y + 8z =1
3x + 4y + 9z =2 |
|
|
.
|

\
|
=
9 4 3
8 5 2
7 6 1
A
|
|
|
.
|

\
|
=
2
1
0
B
|
|
|
.
|

\
|
=
z
y
x
S
In Matlab:
>> A=[ 1 6 7; 2 5 8; 3 4 5]
>> B=[0;1;2];
>> S=inv(A)*B









Verification:

>> det(A)

ans =

0
NO Solution!!!!!
Solving systems of equations in Matlab
Warning: Matrix is singular to working precision.
>>
S =

NaN
NaN
NaN
Applications in mechanical engineering
F1
F2
5N
7N
x
y
60
o
30
o
20
o
80
o
Find the value of the forces F1and F2
F1
F2
5N
7N
x
y
60
o
30
o
20
o
80
o
Projections on the X axis F1 cos(60) + F2 cos(80) 7 cos(20) 5 cos(30) = 0
Applications in mechanical engineering
F1
F2
5N
7N
x
y
60
o
30
o
20
o
80
o
Projections on the Y axis F1 sin(60) - F2 sin(80) + 7 sin(20) 5 sin(30) = 0
Applications in mechanical engineering
F1 cos(60) + F2 cos(80) 7 cos(20) 5 cos(30) = 0
F1 sin(60) - F2 sin(80) + 7 sin(20) 5 sin(30) = 0
F1 cos(60) + F2 cos(80) = 7 cos(20) + 5 cos(30)
F1 sin(60) - F2 sin(80) = - 7 sin(20) + 5 sin(30)
In Matlab:
>> CF=pi/180;
>> A=[cos(60*CF), cos(80*CF) ; sin(60*CF), sin(80*CF)];
>> B=[7*cos(20*CF)+5*cos(30*CF) ; -7*sin(20*CF)+5*sin(30*CF) ]
>> F= inv(A)*B or (A\B)

F =
16.7406
14.6139
In Matlab, sin and cos use radians, not degree
Solution:
F1= 16.7406 N
F2= 14.6139 N
Applications in mechanical engineering
Polynomial Coefficients
The function poly(r)computes the coefficients of the
polynomial whose roots are specified by the vector r.
The result is a row vector that contains the polynomials
coefficients arranged in descending order of power.
For example,

>>c = poly([-2, -5])
c =
1 7 10
2-46
Polynomial Roots
The function roots(a)computes the roots of a polynomial
specified by the coefficient array a. The result is a
column vector that contains the polynomials roots.

For example,

>>r = roots([2, 14, 20])
r =
-2
-5

2-47
Polynomial Multiplication and Division
The function conv(a,b) computes the product of the two
polynomials described by the coefficient arrays a and b.
The two polynomials need not be the same degree. The
result is the coefficient array of the product polynomial.

The function [q,r] = deconv(num,den) computes the
result of dividing a numerator polynomial, whose
coefficient array is num, by a denominator polynomial
represented by the coefficient array den. The quotient
polynomial is given by the coefficient array q, and the
remainder polynomial is given by the coefficient array r.


2-48
Polynomial Multiplication and Division: Examples
>>a = [9,-5,3,7];
>>b = [6,-1,2];
>>product = conv(a,b)
product =
54 -39 41 29 -1 14
>>[quotient, remainder] = deconv(a,b)
quotient =
1.5 -0.5833
remainder =
0 0 -0.5833 8.1667
2-49

Anda mungkin juga menyukai