Академический Документы
Профессиональный Документы
Культура Документы
1
Array Operations
[] Array
y constructor
[ ] Brackets are used to form vectors and matrices.
Example:
A = [ 2 3.5 -5 4 36 7 4 21]
indicate that A is an array of 7 elements also the above can be
written as
A = [ 2 , 3.5 , -5 , 4 , 36 , 7 , 4 , 21]
separating elements with comma or space, and resulting
2 3.5 -5 4 36 7 4 21
3
Vectors and Matrices
y Vectors (arrays) are defined as
y >> v = [1,
[1 2,
2 4, 5]
]
y >> w = [1; 2; 4; 5]
y Matrices (2D arrays) defined similarly
y >> A = [1,2,3;4,-5,6;5,-6,7]
Coll 1
Col 3
Col 2
⎡1 2 3 ⎤ Row 1
⎢⎣5 − 6 7 ⎥⎦ Row 3
4
2
The Colon (:) Operator
1:10 ==>
> [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10]
0:10:50 ==> [0 , 10 , 20 , 30 , 40 , 50]
0:pi/4:pi ==> [0 , 0.7854 , 1.5708 , 2.3562 , 3.1416]
Long Array, Matrix
y >> t =1:10
t=
1 2 3 4 5 6 7 8 9 10
y >> k =2:-0.5:-1
k=
2 1.5 1 0.5 0 -0.5 -1
3
Creating Arrays
y A semicolon as punctuation in the square bracket
operator tells MATLAB to start a new row
>> A = [1, 2, 3; 10, 20, 30]
Addition / subtraction of two vectors
>> v1 = [ 1,
1 3,
3 4]
>> v2 = [ 2, 5, 3 ]
>> v1 + v2
>> v11 – v22
Cn×m = An×m + Bn×m
cij = aij + bij
8
4
Scalar multiplication of a vector
>> v = [ 2,
2 3,
3 4]
>> k = 10
>> k * v
ans =
20 30 40
9
Dot & Cross product
>> u = [2 4 7]
>> v = [10 20 30]
>>w = cross(u, v)
>>w = dot(u, v)
v α
w v.w = ( x1 , x2 ).( y1 , y2 ) = x1 y1 + x2 . y2
10
5
Matrix Index
Indexing using parentheses
y>> A(2,3)
A(2 3)
Index submatrices using vectors
of row and column indices
y>> A([2 3],[1 2])
O de g o d ces s po a !
Ordering of indices is important!
y>> B=A([3 2],[2 1])
y>> B=[A(3,2),A(3,1);A(2,2);A(2,1)]
11
Matrix Index
Index complete row or column using
the colon operator
p
y>> A(1,:)
Can also add limit index range
y>> A(1:2,:)
y>> A([1 2],:)
General notation for colon operator
y>> v=1:5
y>> w=1:2:5
12
6
Matrix Index
⎡5 4 7 6⎤
⎢8 9 1 0⎥⎥
A=⎢
⎢1 2 8 1⎥
⎢ ⎥
⎣7 4 5 3⎦
That is,
is the numbers in the 1st through 3rd rows,
rows 2nd through 4th columns…
columns
A(1:3, 2:4)
13
Matrix Index
The Matrix in MATLAB
Columns
( )
(n)
1 2 3 4 5
A= 4
1
10
6
1
11
6
16
2
21
A (2,4)
1
2 7 12 17 22
2 8 1.2 9 4 25
7
Matrix Index
y The matrix indices begin from 1 (not 0 (as in C))
y The matrix indices must be positive integer
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
15
Concatenation of Matrices
y >> x = [1 2], y = [3 4], z=[ 0 0]
y >> A = [ x y]
1 2 3 4
y >> B = [x ; y]
1 2
3 4
y >> A = 1:4
A = 1 2 3 4
y >> C= repmat(A,3,2)
REPMAT: Replicate and tile an array.
C = [x y ;z]
Error:??? Error using ==> vertcat CAT arguments dimensions are not consistent.16
8
MATLAB Function Reference
disp: (Description) Display text or array
>>disp(A)
displays an array, without printing the array name. If A
contains a text, the string is displayed. Another way to
display an array on the screen is to type its name, but this
prints a leading “A=" which is not always desirable.
>> disp('The Result is'),disp(A)
The Result is
1 2 3
4 -5 6
5 -6 7 17
Built‐in Functions and Operators
y The same types in Vectors exist – with new possibilities
9
Built‐in Functions end and size
y Create an array A by the following:
>> A = [1
[1,2,3,4;
2 3 4 10
10,11,12,13;
11 12 13 20
20, 21
21,22,23]
22 23]
y Replace the last and next to last row/column
elements with [100, 101; 200, 201]
>> A(end-1:end,end-1:end) = [100, 101; 200, 201]
y For vectors, we had length to return the number of
elements.
y For arrays, size built-in function is used: numRow=
3
>> [numRows, numCols] = size(A) numCols=
4
19
Max & Min
Largest component of a matrix A
>> max(A)
(A)
20 21 200 201
>> max(A’)’
4
101
201
>> min(A)
>> min(A’)’
>> min(A,[],1) Smallest component of a matrix A
>> min(A,[],2)
20
10
Mean & Length
Average or mean value of a matrix A
>> mean(A)
10.3333 11.3333 101.0000 102.0000
>> mean(A’)’
2.5000
55.5000
110 5000
110.5000
>> length(A)
Length of vector: The statement length(A) is equivalent to
max(size(A)) for nonempty arrays and 0 for empty arrays.
4
21
Matrix Multiplication
m
Cn× p = An×m Bm× p cij = ∑ aik bkj An×n Bn×n ≠ Bn×n An×n
k =1 22
11
Matrices Operations
Gi
Given A and
d B:
B
23
Deleting Rows and Columns
You can delete rows and columns from a matrix using just a pair of square
brackets. Start with
>>A = [ 16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1 ];
Then, to delete the second column of A, use
>>A(:,2) = [ ]
This changes A to:
24
12
Operators (Element by Element)
¾ .* Element-by-element multiplication
¾ ./ Element-by-element division
¾ .\ Element-by-element left division
¾ .^ Element-by-element power
>> a .* b
ans =
0 5 5 2
>> a ./ b
ans =
0 0.2000 5.0000 0.5000 25
The use of “.” – “Element” Operation
A = [1 2 3; 5 1 4; 3 2 1]
A=
1 2 3
5 1 4
3 2 -1
x= y= b= c= d=
1 2 3 3 4 -1 3 8 -3 0.33 0.5 -3 1 4 9
K= x^2
Erorr: ??? Error using ==> ^ Matrix must be square.
B=x*y
Erorr: ??? Error using ==> * Inner matrix dimensions must agree. 26
13
The Transpose Operator
The transpose
p operator
p is used to flip
p an array.
y
Examples :
y More formally, if A is an NxM ⎡1 4⎤
⎡1 0 − 7 ⎤
vector, then A' will be an MxN If A = ⎢ ⎥ then T ⎢
A = ⎢ 0 15⎥⎥
⎣4 15 2 ⎦ ⎢⎣− 7 2 ⎥⎦
array whose elements are
⎡2⎤
defined byy A'(i,j)
( j) = A(j,i).
(j )
If B = ⎢⎢4⎥⎥ then
h BT = [2 4 6]
y The effect of applying the ⎢⎣6⎥⎦
transpose operator to an array is ⎡ 1 2 3⎤ ⎡1 4 7⎤
If C = ⎢⎢4 5 6⎥⎥ then C T = ⎢⎢2 5 8 ⎥⎥
to flip rows and columns.
⎣⎢7 8 9⎥⎦ ⎣⎢3 6 9 ⎥⎦
27
The Transpose Operator
>> A = [1,2,3,4; 10,11,12,13; 20, 21,22,23]
>> T= A
A’
y The effect of applying the transpose operator to an array is to flip
rows and columns.
14
Flip Matrix
flip matrix about vertical axes:
>> B=fliplr(A)
29
Matrix Left Division
y A linear system of equations can be modeled as:
3x + 2 y − z = 10 ⎡ 3 2 − 1⎤ ⎡ x ⎤ ⎡10 ⎤
− x + 3y + 2z = 5 ⇒ ⎢− 1 3 2 ⎥ × ⎢ y ⎥ = ⎢ 5 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
x − y − z = −1 ⎢⎣ 1 − 1 − 1⎥⎦ ⎢⎣ z ⎥⎦ ⎢⎣− 1⎥⎦
In other words…
v v v v v v
Ax = y ⇒ x = A −1 y ⇒ x=A\y
30
15
Solutions to Systems of Linear Equations
y Solution by Matrix Inverse: y Solution by left division:
Answer: Answer:
x1 = -2, x2 = 5, x3 = -6 x1 = -2, x2 = 5, x3 = -6
Matrix Functions
y The square root of the elements of A:
>> As=sqrt(A)
16
Matrix Functions
Many elementary matrices predefined
y >> help elmat;
Elementary matrices.
matrices
zeros - Zeros array.
ones - Ones array.
eye - Identity matrix.
repmat - Replicate and tile array.
rand - Uniformly distributed random numbers. (0-1)
randn - Normally distributed random numbers.
linspace - Linearly spaced vector.
logspace - Logarithmically spaced vector.
freqspace - Frequency spacing for frequency response.
meshgrid - X and Y arrays for 3-D plots.
: - Regularly spaced vector and index into matrix.
33
Matrix Functions
Many elementary matrices predefined
y >> help elmat;
Basic array information.
information
size - Size of array.
length - Length of vector.
ndims - Number of dimensions.
numel - Number of elements.
disp - Display matrix or text.
isempty - True for empty array.
isequal - True if arrays are numerically equal.
isequalwithequalnans - True if arrays are numerically equal.
isnumeric - True for numeric arrays.
islogical - True for logical array.
logical - Convert numeric values to logical.
34
17
Generating Vectors from functions
y eye(M) MxM matrix of eydentety x = eye(3,3)
x =
1 0 0
0 1 0
0 0 1
IA = AI = A 35
Generating Vectors from functions
y zeros(M,N) MxN matrix of zeros x = zeros(1,3)
y zeros(M) MxM matrix of zeros x =
0 0 0
18
Eigenvalues
The eigenvalues of the matrix A is .
g( )
>> e = eig(A)
e=
34.0000
8.0000
0.0000
-8.0000
Reduced row echelon form
R = rref(A) produces the reduced row echelon form of A using
Gauss Jordan elimination with partial pivoting.
A default tolerance of (max(size(A))*eps *norm(A,inf)) tests for
negligible column elements.
>> R=rref(A)
R=
1 0 0 1
0 1 0 -3
0 0 1 3
0 0 0 0
38
19
Determination
A must be square
⎡a a12 ⎤ a11 a12
det ⎢ 11 = = a11a22 − a21a12
⎣a21 a22 ⎥⎦ a21 a22
Determination
The determinant of this particular matrix happens to be zero,
indicating that the matrix is singular.
>> d = det(A)
d=
0
Since the matrix is singular, it does not have an inverse. If you try
to compute the inverse with
>> X = inv(A)
you will get a warning message
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 9.796086e-018. 40
20
Matrices: Magic Squares
Interestingly,
Durer also dated
this engraving
by placing 15
and 14 side-by-
side in the
magic square.
41
The magic Function
A = magic(3) B = magic(4)
A = B =
8 1 6 16 2 3 13
3 5 7 5 11 10 8
4 9 2 9 7 6 12
4 14 15 1
This is called a magic square because the sum of the elements in each column is the same.
>> sum(A)
15 15 15
And the sum of the elements in each row, obtained by transposing twice, is the same.
>> sum(A
sum(A')'
)
15
15
15
This is also a special magic square because the diagonal elements have the same sum.
>> sum(diag(A)) >> trace(A)
>> sum(diag(fliplr(A)))
15
42
21
3D Matrices
>> c = [1 2 3; 4 5 6; 7 8 9;... >> d = reshape(c, 3,3,2) >> size(d)
2 2 2; 3 3 3; 4 4 4]’
d(:,:,1) = ans =
c= 3 3 2
1 4 7
1 4 7 2 3 4 2 5 8
2 5 8 2 3 4 3 6 9
3 6 9 2 3 4
d(:,:,2) =
RESHAPE:
>> size ( c) 2 3 4
2 3 4 Change size
3 6
2 3 4
43
3D Matrices
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0 Page N
16 20 30 130 0
0 100 80 0
1 15 11
1 1
9 70 60 120 0
1 2 3 4
4 14 15 1
1 3 6 10
1 4 10 20
Page 1
44
22
Useful Matrix Functions
For complete list, see :
Help
MATLAB Help
Mathematics
Matrices and Linear Algebra
Function Summary
45
Graphics
>> x = 0:pi/20: 2*pi;
>> y = sin(x);
>> plot(x,y,’b-x’); % blue line, x’s
>> hold; % keep lines
>> z = cos(x);
>> plot(x,z,’r:+’); % dotted red, +’s
1
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
46
0 1 2 3 4 5 6 7
23