Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Matrix and Matrix

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

⎡1 3⎤ ⎡ 1 0.1 4 ⎤ ⎡ 1.1 − 5.6 5.3⎤ ⎡ 1 − 2.3⎤


S=⎢
M=⎢ ⎥ N = ⎢⎢13 2 − 2.3⎥⎥
⎣− 0.3

10 6.3⎥⎦ T = ⎢ 5.5 6.9 ⎥⎥
⎣10 16 ⎦ ⎢⎣ 5 6.5 3 ⎥⎦ ⎢⎣− 0.2 63.4 ⎥⎦

n  Matrix with only one row or column is called


vector
⎡1.1⎤ W = [− 1.2 0 12.25]
V=⎢ ⎥
⎣ − 2⎦
3
Defining Vector and Matrix
n  Enter matrix as a list of elements:
¨  Separate the elements of a row with blank or commas (,).
¨  Use a semicolon (;) to indicate the end of each row.
¨  Surround the entire list of elements with square brackets, [ ].
n  Examples ⎡ 1 0.1 4 ⎤
⎡1 3⎤ N = ⎢⎢13 2 − 2.3⎥⎥
M=⎢ ⎥
⎣10 16 ⎦ ⎢⎣ 5 6.5 3 ⎥⎦
n  M = [1 3; 10 16]; N = [1 0.1 4; 13 2 -2.3; 5 6.5 3];
⎡ 1 − 2.3⎤
⎡ 1.1 − 5.6 5.3⎤ T = ⎢⎢ 5.5 6.9 ⎥⎥
S=⎢ ⎥
⎣ − 0.3 10 6.3⎦ ⎢⎣− 0.2 63.4 ⎥⎦
n  S = [1.1 -5.6 5.3; -0.3 10 6.3]; T = [1 -2.3; 5.5 6.9; -0.2 63.4];
4
Defining Vector and Matrix
n  Examples
⎡1.1⎤
V=⎢ ⎥
⎣ − 2⎦
n  V = [1.1; -2];

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 = ⎢⎢13 2 − 2.3⎥⎥ 2 Note:


• Index starts from 1
⎢⎣ 5 6.5 3 ⎥⎦ 3 • Must be an integer

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)]

n  Extracting row from matrix j 1 2 3


i
¨  D = M(2, :)
⎡ 1 0.1 4 ⎤ 1
n  Extracting elements from row ⎢ ⎥
¨  E = M(3, 2:3)
M = ⎢13 2 − 2.3⎥ 2

¨  F = M(3, [1, 3, 2, 1]) ⎢⎣ 5 6.5 3 ⎥⎦ 3


F = [M(3,1), M(3,3), M(3,2), M(3,1)]

G = M([3, 2], [3, 1])


G = [M(3, 3), M(3, 1); M(2, 3), M(2, 1)] 9
Matrix Addressing and Indexing (2)
n  Consider a single-row vector of M = [− 2 0 5.2 6.3]
¨  A = M(1);
¨  B = M(2);
¨  C = M([1 2 4]);
¨  D = M(1:3) ⎡ 4 .2 ⎤
⎢ −3 ⎥
n  N=⎢
Consider a single-column vector of ⎥
¨  E = N(1); ⎢ 2 .6 ⎥
⎢ ⎥
¨  F = N(2);
⎣ − 8 .5 ⎦
¨  G = N([1 4 2]);
¨  H = N(1:3)
10
Questions ⎡1 −3 ⎤
n  Which one of the following statements is to create the matrix of S = ⎢⎢− 6 10 ⎥⎥ ?
¨  A. S = [1 -3 -6; 10 3.3 -2.3]; ⎢⎣3.3 − 2.3⎥⎦
¨  B. S = [1 -3; -6 10; 3.3 -2.3];
¨  C. S = [1 -6; 3.3 -3; 10 -2.3];
¨  D. S = [1 -6 3.3; -3 10 -2.3];
n  What is the value for S(3, 2)?
¨  A. -6 B. 3.3 C. 10 D. -2.3
n  What is the value for S(1, 1) + S(2, 1) + S(3, 1)?
¨  A. -6 B. 14.3 C. -1.7 D. 12.5
n  Considering M = S(:, 1), which of the following matrix is M?
¨  A. M = [− 6 10] B. M = [3.3 − 2.3] C. ⎡ − 3 ⎤ D. ⎡1⎤
⎢ ⎥ ⎢ ⎥
M = ⎢ 10 ⎥ M = ⎢ − 6⎥
⎢⎣− 2.3⎥⎦ ⎢⎣3.3⎥⎦

n  Which of the following statement will give M = [1 − 3] ?


¨  A. M = S(1, 2)B. M = S(:, 1) C. M = S(1, :) D. M = (1:3, 1)
11
Useful Built-in Function for Matrix
n  Transpose (Apostrophe operator ‘)
¨  Performs a complex conjugate transposition
¨  Flips a matrix about its main diagonal and also change the sign of
the imaginary component of any complex element.
n  To make the transpose of a matrix is to simply make the i th
row of the matrix the i th column.
⎡1 3⎤ ⎡1 10 ⎤ Note: .’ is the transpose
M=⎢ ⎥ M' = ⎢ ⎥
⎣10 16⎦ ⎣3 16 ⎦

⎡1 + 2 j 3 − j ⎤ ⎡1 − 2 j 10 ⎤
N=⎢ ⎥ N' = ⎢ ⎥
⎣ 10 16 ⎦ ⎣ 3 + j 16 ⎦

⎡ 1.1 − 5.6 5.3⎤ ⎡ 1.1 − 0.3⎤


S=⎢ ⎥
⎣− 0.3 10 6.3⎦ S' = ⎢⎢− 5.6 10 ⎥⎥
⎢⎣ 5.3 6.3 ⎥⎦ 12
Useful Built-in Function for Matrix
n  size
¨  Command: [r, c] = size(M);
n  Return two scalars, r and c, containing the number of rows
and columns, respectively

⎡ 1.1 − 5.6 5.3⎤


S=⎢ ⎥
⎣− 0.3 10 6.3⎦
S = [1.1, -5.6, 5.3; -0.3, 10, 6.3]
n  [r, c] = size(S);
¨  r is 2
¨  c is 3
¨  S is a 2-by-3 matrix
13
Question: Write a script-M file which reads in a
matrix M and displays the number of elements in
M.

M = input('Please enter matrix M: ');

[r, c] = size(M);

disp('Number of elements in M is');

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 rules for subtraction are exactly the


same as for addition but with minus signs
replacing the plus signs.
n  That is A - B is seen as A +(-B).
19
Matrix Arithmetic (1)
n  Multiplication
¨ C =A×B
n  Two matrices can be multiplied only when their
dimensions are of the form in the following figure
¨  that
is when the dimensions are written side to side the
two red ones are the same

¨ 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

• Applies to square matrix only 22


Matrix Arithmetic (4)
Element-by-Element operation Representative Data
A = [a1 a2 … an];
B = [b1 b2 … bn];
c (a scalar, e.g., 1, 2, 3.2)
Scalar addition A+c = [a1+c a2+c … an+c]
Scalar subtraction A-c = [a1-c a2-c … an-c]
Scalar multiplication A*c = [a1*c a2*c … an*c]
Scalar division A/c = [a1/c a2/c … an/c]

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

Format >> det(A)


ans =
zeros (n, m) -2
• Generates an n-by-m matrix of zeros

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

A{1,1} = [1 4 3; 0 5 8; 7 2 9]; Cell 1,1 Cell 1,2


A{1,2} = ‘Computer’; 1 4 3
‘Computer’
A{2,1} = 3 + 7i; 0 5 8
7 2 9
A{2,2} = [-3 2 4];
Cell 2,1 Cell 2,2

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.

Cell 1,1 Cell 1,2 Questions:


Cell A Cell A
>> B{1,3}(2,3)
>> B{2,1}(1,1)
>> B{1,4}(1,5)
>> B{2,2}(2)
>> B{2,2}(1,2)
>> B = [A A]
B=
[3x3 double] 'Computer' [3x3 double] 'Computer'
[3.0000+ 7.0000i] [1x3 double] [3.0000+ 7.0000i] [1x3 double]
34
Character Strings
n  Character strings
¨  Special numerical arrays of ASCII values that are
displayed as their character-string representation
¨  An array of characters
n  Enter text into MATLAB using single quotes
¨  Single character, e.g., ch = ‘a’;
¨  String, e.g., str = ‘bcdef’;

n  Concatenation with square brackets, []


¨  F = [ch, str, ‘ABCDE’]; It forms a single-
¨  H = [‘abcde’; ‘ABCDE’]; row string

• It forms a 2x5 matrix


• String in each row must be in the same
dimension (length) 35
Cell Array for String >> str{1,1}= ('Computer');
>> str{1,2}= ('Engineering');
cell(1,1) cell(1,2) >> str{2,1}= ('MATLAB');
‘Computer’ ‘Engineering’ >> str{2,2}= ('C Program');

>>str
cell(2,1) cell(2,2) str =
‘MATLAB’ ‘C Program’ 'Computer' 'Engineering'
'MATLAB' 'C Program'

>> str1{1}= ('Computer');


>> str1{2}= ('Engineering');
1-by-4 cell
>> str1{3}= ('MATLAB');
>> str1{4}= ('C Program');

>>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.

>> patient.name = ‘ABCDE';


patient
>> patient.billing = 127.00;
>> patient.test = [79 -12 7; 1.8 5 6; 23 -7 2];

name e.g., ABCDE


>> patient.name
ans =
billing e.g., 127.00 ABCDE

test e.g., 79 -12 7 >> patient.name = ‘ABC12’;


1.8 5 6 >> patient.name
23 -7 2 ans =
ABC12 37
n  patient is an array containing a structure with three fields.
¨  To expand the structure array, add subscripts after the structure name.
>> patient.name = ‘ABCDE';
patient(1)
>> patient.billing = 127.00;
>> patient.test = [79 -12 7; 1.8 5 6; 23 -7 2];
name e.g., ABCDE
patient array

>> patient(2).name = ‘DE123';


billing e.g., 127.00 >> patient(2).billing = 223.10;
test e.g., 79 -12 7 >> patient(2).test = [6 -2 1; 8 5 3.3; 66 -9 11];
1.8 5 6
23 -7 2 >> patient(1).name
ans =
ABCDE

patient(2) >> patient(2).name


ans =
name e.g., DE123 DE123

billing e.g., 223.10 >> patient(2).test(1,2)


test ans =
e.g., 6 -2 1
-2
8 5 3.3
66 -9 11
38

You might also like