0% found this document useful (0 votes)
51 views20 pages

MATLAB Chapter 2: Numeric and Matrix Arrays

This document summarizes key concepts about numeric arrays and matrices in MATLAB. It discusses how to define vectors and matrices, their basic properties like size and length, and how to perform operations on them like transpose and element-by-element operations. It also covers polynomial operations in MATLAB such as adding, multiplying, and dividing polynomials using functions like conv, deconv, polyval, and operations on special matrices like identity and zero matrices.

Uploaded by

HusseinHazime
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views20 pages

MATLAB Chapter 2: Numeric and Matrix Arrays

This document summarizes key concepts about numeric arrays and matrices in MATLAB. It discusses how to define vectors and matrices, their basic properties like size and length, and how to perform operations on them like transpose and element-by-element operations. It also covers polynomial operations in MATLAB such as adding, multiplying, and dividing polynomials using functions like conv, deconv, polyval, and operations on special matrices like identity and zero matrices.

Uploaded by

HusseinHazime
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

MATLAB Chapter 2

Numeric and Matrix Arrays


2.1 Arrays

■ x=[1 2 4] 1x3 line vector (1 row and 3 columns)


■ x=[1;2;4] 3x1 column vector (3 rows and 1 column )
■ length(x) gives the number of elements in vector x

■ A=[1 2;4 2;7 -1] 3x2 matrix (3 rows and 2 columns )


■ size(A) gives the number of rows and columns in matrix A

■ B=[1 2 3]; C=[4 5 6];


■ D1=[B C]; 1x6 line vector D1=[1 2 3 4 5 6]
■ D2=[B;C]; 2x3 matrix D2=[1 2 3;4 5 6]

2
2.1 Arrays

■ x=(0:2:10); returns vector x=[0 2 4 6 8 10];


■ Initial value 0, step 2 and final value 10

■ z=(10:-2:0); returns vector z=[10 8 6 4 2 0];

■ By default if omitted step=1

3
2.1 Matrix Transpose

■ A=[1 2 3 4; 5 6 7 8; 9 10 11 12]; 3x4 matrix

■ B=A’; Matlab transpose matrix given by


■ B=[1 5 9; 2 6 10; 3 7 11; 4 8 12]; 4x3 matrix

■ For array with complex elements


■ C=[1+1i 2 3 4; 5 6 7 8; 9 10 11 12-3i];
■ D=C’; produces complex conjugate transpose
■ D=[1-1i 5 9; 2 6 10; 3 7 11; 4 8 12+3i];

■ E=C.’; produces only the transpose matrix


■ E=[1+1i 5 9; 2 6 10; 3 7 11; 4 8 12-3i];
4
2.2 Array Addressing

■ v=(0:0.2:1); v(5)=0.8 element of vector v with index 5

■ A=[1 2 3 4; 5 6 7 8; 9 10 11 12];

A(2,3)=7 element of matrix A at row 2 and column 3

A(1:2:end,:) submatrix of matrix A with odd rows

A(:,2:2:end) submatrix of matrix A with even columns

A(1:3,3:4) submatrix of matrix A with rows one to three


and columns three to four

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(2:3,1:3) denotes submatrix


6
of A with elements in rows 2
to 3 and columns 1 to 3
2.2 Other array manipulations

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

%repeats row 2 in D three times

14
2.3 Useful Array Functions

■ Array functions from Appendix A


Examples:

◻ max(A) returns the largest element in A if A is a vector.


Returns a row vector containing the largest
elements in each column of A if A is a matrix…
◻ [x,k]=max(A)
similar to max(A) but stores the maximum
values in the row vector x and their indices in
◻ sum(A) the row vector k.
sums the elements in each column of the array A
◻ sort(A) and returns a row vector containing the sums.
sorts each column of the array A in ascending
order and returns an array the same size as A.
8
2.3 Element by Element Operations

■ Table 2.3-1 Element-by-element (e-b-e) operations


Examples
◻+ Scalar-array addition A+b
◻+ Array addition A+B
◻ .* Array e-b-e multiplication A.*B
◻ ./ Array e-b-e division A./B

■ Remember, A*B is Matrix multiplication and not e-b-e


multiplication (focus on DIMENSIONS issue)
9
2.3 Matrix Operations
Matrix multiplication A*B is done directly in Matlab.

Dimension issue:
Number of A columns SHOULD be equal to number of B rows!!!!!!

>> A=[6,-2 ; 10,3; 4,7];


>> B=[9;-5];
>> A*B

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

■ Matrix multiplication is associative and distributive:


A*(B+C)=A*B+A*C
A*(B*C)=(A*B)*C
Example:
Same A and B matrixes
C=[9 8 7; 6 5 4; 3 2 1];

11
2.3 Special Matrices

■ Special matrices (Appendix A) Examples:


◻ eye(n) creates an nxn identity matrix
◻ eye(size(A)) creates an identity matrix the
same size as the matrix A

◻ ones(n) creates an nxn matrix of ones


◻ ones(m,n) creates an mxn array of ones
◻ zeros(n) creates an nxn matrix of zeros

12
2.3 Special Matrices

■ When A is a square matrix, then raising the matrix to a power is


equivalent to repeatedly multiplying the matrix by itself:

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

■ A polynomial can be described in Matlab >> roots([1 -7 40 -34])


with an array whose elements are the
polynomial coefficients, starting with ans =
coefficient with the highest power of x 3.0000 + 5.0000i
3.0000 - 5.0000i
■ Use the roots command to solve 1.0000
for roots >> poly([1 3-5i 3+5i])

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

■ To add polynomials, add the arrays that describe their


coefficients.
■ If polynomials are of different degrees, add zeros to
the coefficient array of the lower-degree polynomial.
>> f=[9 -5 3
7];
Example: >> g=[0 6 -1
2];
◻ f(x) = 9x3 – 5x2 +3x +7
ans
>> f+g
◻ g(x) = 6x2 – x + 2 =
◻ f(x) + g(x) = 9x3 + x2 +2x +9 9 1 2 9

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

■ Dividing f(x) by g(x) gives


3 2
f(x) 9x – 5x + 3x + 7
g(x)= 6x2 – x + 2 = 1.5x − 0.5833

with a remainder of -0.5833x+8.1667.

■ In Matlab, polynomial multiplication and division can be done using


the functions:
conv(a,b)
[q,r]=deconv(num,den)
16
2.4 Polynomial Multiplication and Division

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

■ The polyval(a,x) function


evaluates a polynomial at specified
values of its independent variable x,
which can be a matrix or a vector.
The polynomial’s coefficient array is
a. The result is the same size as x.
Example:
◻ f(x) = 9x3 – 5x2 +3x +7
◻ x=0:2:10

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

■ Polynomial functions from Appendix A

◻ conv(a,b) computes the product of two


polynomials
computes the value of polynomial a in
◻ polyval(a,x) specific value or vector x

19
Test Your Understanding

20

You might also like