Professional Documents
Culture Documents
Matrix and Matrix Operation
Matrix and Matrix Operation
Operation
1
Learning Objectives
n Define vectors and matrices
n Matrix addressing and indexing
n Matrix Arithmetic
n Built-in matrix functions
n Cell array, structure
2
Defining Vector and Matrix
n Matrix is a rectangular array of number
¨ A scalar is regarded as a 1-by-1 matrix
W = [− 1.2 0 12.25]
n W = [-1.2 0 12.25];
5
Colon Operator Format
start : increment : end
n Colon operator (:)
¨ One of the most important MATLAB operators
¨ Example
n 1:10
¨ A row vector containing the integers from 1 to 10
¨ Starts from 1, counts up by 1, and stops at 10
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> A = 1:10
A=
1 2 3 4 5 6 7 8 9 10
6
Colon Operator (1)
n To obtain non-unit spacing, specify an
increment
n Examples
¨ 100: -7: 50 (starts with 100, count down by -7, until the number just >= 50)
>> 100: -7: 50 In the range of 50 and
ans = 100 inclusive
100 93 86 79 72 65 58 51
¨ -pi: 0.8: pi (starts with -pi, count up by 0.8, until the number just <= pi)
A = -pi: 0.8: pi
A=
-3.1416 -2.3416 -1.5416 -0.7416 0.0584 0.8584 1.6584 2.4584 7
Matrix Addressing and Indexing
M(1,1) is 1 M(1,3) is 4
n M(i, j), M is a variable name for a matrix
¨ The element in row i and column j
j 1 2 3 M(3,3) is 3
i
⎡ 1 0.1 4 ⎤ 1
M(3,2) is 6.5
8
Matrix Addressing and Indexing (1)
n Extracting column from Matrix
¨ A = M(:,1)
n Extracting elements from column
¨ B = M(1:2, 2)
¨ C = M([1, 2], 2); C = [M(1,2); M(2,2)]
⎡1 + 2 j 3 − j ⎤ ⎡1 − 2 j 10 ⎤
N=⎢ ⎥ N' = ⎢ ⎥
⎣ 10 16 ⎦ ⎣ 3 + j 16 ⎦
[r, c] = size(M);
disp(r*c);
14
Concatenation
n Concatenation is the process of joining small
matrices to make bigger ones.
¨ [ ] is the concatenation operator
n Example A=⎢
⎡1 2 ⎤
6 8 ⎥
¨ A = [1 2; 6 8]; ⎣ ⎦
⎡7 ⎤
¨ B = [7 9]; B = [7 9] B' = ⎢ ⎥
⎣9 ⎦
¨ C = [1]; C = [1]
¨ D = [A B’; B C];
⎡ ⎤
D = ⎢⎢ ⎥
⎥
⎢⎣ ⎥⎦
15
Deleting Rows and Columns Extending Rows and Columns
n [ ] can be used to delete rows
or columns >> A=[1 2 3; 2 3 4]
A =
>> A=[1 2 3; 2 3 4] 1 2 3
A = 2 3 4
>> A(4,:)=[7 8 9]
1 2 3 A =
2 3 4 1 2 3
2 3 4
>> A(:,2) 0 0 0
ans = 7 8 9
2 Modification
3 >> A=[1 2 3; 2 3 4]
A =
1 2 3
>> A(:,2)=[] 2 3 4
A = >> A(1,2)=-10
A =
1 3
1 -10 3
2 4 2 3 4 16
Format
Format length (x)
linspace (x1, x2, N) • Returns the length of vector X
• Generates N points between X1 and X2.
• For N < 2, linspace returns X2. Example
>> A=[1 2 3]
A =
Example 1 2 3
>> A = linspace(-10, 10, 5) >> length(A)
ans =
A=
3
-10 -5 0 5 10
>> B=[1 2 3; 2 3 4]
Format B =
1 2 3
2 3 4
logspace (x1, x2, N) >> length(B)
• Generates a row vector of N ans =
logarithmically equally spaced points 3
between decades 10X1 and 10X2.
>> C=[1 2; 2 3; 6 7]
• For N < 2, logspace returns 10X2. C =
1 2
Example 2 3
6 7
>> logspace(-1, 1, 5) >> length(C)
ans = ans =
0.1000 0.3162 1.0000 3.1623 10.0000 3 17
Question
Write a MATLAB script which
• Reads a 2-by-2 matrix (using input function)
• Finds and displays the determinant.
⎡ a11 a12 ⎤
A=⎢ ⎥
⎣a21 a22 ⎦
• det(A) = a11 × a22 – a12 × a21
18
Matrix Arithmetic
n Addition and Subtraction
¨ C = A + B (in the same dimension)
n theaddition of the numbers in the corresponding
positions of the two matrices, i.e., when C = A + B
¨ The dimension of C: m × p
20
Matrix Arithmetic (2)
n Matrix Multiplication is done in the following steps
¨ Pick the first row of the first matrix and the first column of the second matrix.
¨ Then, starting with the first element in each, multiply them together, do the same
with each consecutive pair of elements adding the resulting multiples as you go.
21
Matrix Arithmetic (3)
n MATLAB Code
>>A = [1 2 3; 7 8 9];
>>B = [4 10; 5 11; 6 12];
>>A*B Division
ans = C = A/B
32 68 • equivalent to C = A*B-1
122 266 • B -1 is the inverse of B
23
Matrix Arithmetic (5) – dot operator
Element-by-Element operation Representative Data
A = [a1 a2 … an];
B = [b1 b2 … bn];
c (a scalar, e.g., 1, 2, 3.2)
Array addition A+B = [a1+b1 a2+b2 … an+bn]
Array subtraction A-B = [a1-b1 a2-b2 … an-bn]
Array multiplication (elementwise) A.*B = [a1*b1 a2*b2 … an*bn]
Array division (elementwise) A./B = [a1/b1 a2/b2 … an/bn]
Array exponentiation (elementwise) A.^c = [a1^c a2^c … an^c]
Mathematically,
A^n = An = A*A* … *A (n times)
A.^B = [a1^b1 a2^b2 … an^bn]
Not the same as A.^n c.^A = [c^a1 c^a2 … c^an]
24
Questions
Given A = [1 2], B = [3 4] and C = 5.
n Evaluate A’?
¨ A. [1 2] B. [1; 2] C. [3 4] D. [3; 4]
n Considering the MATLAB statement of D = [C A; B C],
which of the following is D?
¨ A. [5 1; 2 3; 4 5] B. [5 3 4; 1 2 5]
¨ C. [5 1 2; 3 4 5] D. [5 3; 4 1; 2 5]
n Evaluate A + B.
¨ A. [1 2 3 4] B. [3 4 1 2]
¨ C. [4 6] D. [6 4]
n Considering the MATLAB statement of D = 0:0.4:1,
which of the following is D?
¨ A. 0 0.4 0.8 1.2 B. 0.4 0.8
¨ C. 0.4 0.8 1.2 D. 0 0.4 0.8 25
Questions
Write a MATLAB script which evaluates the function: h(t) =
(sin(t)cos(t))n where t is a user input vector and n is a
scalar.
26
Further matrix functions
Format Format
ones (n, m) det (X)
• Generates an n-by-m matrix of ones • Determinant of a square matrix X
Example Example
>> A = ones(2,3) >> A=[1 2; 3 4]
A = A =
1 1 1 1 2
1 1 1 3 4
Example
>> A = zeros(2,3)
A =
0 0 0
0 0 0 27
Further matrix functions
[Y,I] = max(X)
• Returns the maximum element in vector X
• Returns the indices of the maximum values in vector I.
• If the values along the first non-singleton dimension contain more than
one maximal element, the index of the first one is returned.
[Y,I] = min(X)
• Y: The minimum element in vector X
• I: The index of the minimum element
mean(X)
• Returns the average value of all values in a one-dimensional array X
sum(X)
• Returns the sum of the elements of one-dimensional array X
28
Further matrix functions
Format Format
[V, D] = eig (X) inv (X)
• Produces a diagonal matrix D of • Inverse of the square matrix X
eigenvalues and a full matrix V
whose columns are the Example
corresponding eigenvectors. >> A=[1 2; 3 4]
• X must be square A =
1 2
Example 3 4
>> A=[1 2; 3 4]
A = >> inv(A)
1 2 ans =
3 4 -2.0000 1.0000
>> [V, D] = eig(A) 1.5000 -0.5000
V =
-0.8246 -0.4160
0.5658 -0.9094
D =
-0.3723 0
For further information:
0 5.3723 help matfun 29
Question
Solve simultaneously for x and y:
ax + by = c
dx + ey = f
where a, b, c, d, e, f are the user-input scalar coefficients
⎡ a b⎤ ⎡ x ⎤ ⎡ c ⎤ ⎡ x ⎤ ⎡ a b⎤ ⎡ c ⎤
−1
⎢d e ⎥ ⎢ y ⎥ = ⎢ f ⎥ ⎢ y ⎥ = ⎢d e ⎥ ⎢ f ⎥
⎣ ⎦⎣ ⎦ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦
30
Cell Array
n Cell array
¨ MATLAB array – whose element are cells
n Each cell is a cell array which can hold any data types, e.g.,
character, strings, other cell array and structure
¨ provides a storage mechanism for dissimilar kinds of
data.
n You can store arrays of different types and/or sizes within the
cells of a cell array.
¨ For example, you can store a 1-by-50 char array, a 7-by-13
numerical array
31
Creating a Cell Array
n Curly brace { }
¨ To access or specify the content of cells
3 + 7i -3 2 4
32
Retrieving Cell Array Content
>> A >> A{1,1}
A= ans =
[3x3 double] 'Computer' 1 4 3 >> A{1,2}(1)
[3.0000+ 7.0000i] [1x3 double] 0 5 8 ans =
7 2 9 C
>> celldisp(A) >> A{1,1}(1,1)
ans = >> A{1,2} >> A{1,2}(2)
A{1,1} = 1 ans = ans =
1 4 3 Computer o
0 5 8 >> A{1,1}(1,2)
7 2 9 ans = >> A{2,1} >> A{1,2}(3) = 'M';
4 ans =
A{2,1} = 3.0000 + 7.0000i >> A{1,2}
3.0000 + 7.0000i >> A{1,1}(1,3) = 99;
ans =
>> A{1,1} >> A{2,2} CoMputer
A{1,2} = ans = ans =
Computer 1 4 99 -3 2 4
0 5 8
A{2,2} = 7 2 9
-3 2 4 33
Building Cell Arrays with Concatenation
• Concatenate the contents of the cells into a new array
using the square bracket [ ] operator.
>>str1
str1 =
'Computer' 'Engineering' 'MATLAB' 'C Program' 36
Structure
n Structures are MATLAB arrays with named "data containers" called
fields.
¨ The fields of a structure can contain any kind of data.