MATLAB Chapter 2: Numeric and Matrix Arrays
MATLAB Chapter 2: Numeric and Matrix Arrays
2
2.1 Arrays
3
2.1 Matrix Transpose
■ A=[1 2 3 4; 5 6 7 8; 9 10 11 12];
5
2.2 Array Addressing
■ Examples:
>> A
◻ v(2:5) represents elements
A =
2 to 5 of vector v
1 2 3 4
5 6 7 8
◻ A(:,3) denotes all elements 9 10 11 12
in column 3 of A
>> B=A(2:3,1:3)
B =
◻ A(:,2:4) denotes all elements
in columns 2 to 5 of matrix A 5 6 7
9 10 11
A =
6 9 4 D =
1 5 7
3 8 5
>>B=A(:,end:-1:1) 2 -6 9
B = >> E=D([2,2,2],:)
4 9 6 E =
7 5 1
2 -6 9
2 -6 9
%read matrix A from right to left 2 -6 9
14
2.3 Useful Array Functions
Dimension issue:
Number of A columns SHOULD be equal to number of B rows!!!!!!
ans =
64
75
1
10 20
2.3 Matrix Operations
■ Matrix multiplication is not commutative:
AB ≠ BA
Example:
A=[1 2 3; 4 5 6; 7 8 9];
B=[10 11 12; 13 14 15; 16 17 18];
Result: A*B=[84 90 96; 201 216 231; 318 342 366];
B*A=[138 171 204; 174 216 258; 210 261 312];
11
2.3 Special Matrices
12
2.3 Special Matrices
◻ A^2 = A*A
◻ A^3 =A*A*A
◻ Give Example with A=[1 2 3; 4 5 6; 7 8 9];
13
2.4 Polynomial Operations (Appendix A)
ans =
■ Example: f(x) = 1x3 − 7x2 + 40x − 34 =
0 1 -7 40 -34
■ To find the polynomial from its roots, use
>>
the function poly([root1,root2,…])
14
2.4 Polynomial Addition and Subtraction
15
2.4 Polynomial Multiplication and Division
■ The product of the polynomials f(x) and g(x) is
f(x)g(x) = (9x3 – 5x2 +3x +7)(6x2 – x + 2)
= 54x5 – 39x4 + 41x3 + 29x2 – x + 14
>> f=[9,-5,3,7];
>> g=[6,-1,2];
Notice: Unlike >> product=conv(f,g)
during addition or product =
subtraction,
polynomials don’t 54 -39 41 29 -1 14
need to have the
same degree >> [quotient,remainder]=deconv(f,g)
during quotient =
multiplication or
division (i.e. no 1.5000 -0.5833
need to add
zeros) remainder =
0 0 -0.5833 8.1667
17
2.4 Plotting Polynomials
>> a=[9,-5,3,7];
>> x=0:2:10;
>> f=polyval(a,x);
>> plot(x,f),xlabel('x'),ylabel('f(x)'),grid
18
2.6 Polynomial functions
19
Test Your Understanding
20