0% found this document useful (0 votes)
26 views

Arrays in Matlab

Uploaded by

Thanh Phúc Hồ
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)
26 views

Arrays in Matlab

Uploaded by

Thanh Phúc Hồ
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/ 44

Arrays

Arrays

➢Arrays
➢Two-dimensional Arrays
➢Element-by-element Operations
➢Matrix Operations
➢Polynomial Operations Using Arrays
▪ The array is the fundamental form of Matlab,
uses to store data.
▪ Scalars: one row and one column (special case)
▪ Vectors:
✓ Row : one row and multiple columns
✓ Column: multiple rows and one column
▪ Matrices: multiple rows and multiple columns
In Matlab, a vector, or any list of numbers, can be
entered in a horizontal (row) or vertical (column)
vectors.
Ex: the data from the previous slide can be entered in
rows:[1984 1986 1988 1990 1992 1994 1996]
[127 130 136 145 158 178 211]
or in column: 1984  127 
1986  130 
   
1988  136 
   
1990  145 
1992  158 
   
1994  178 
1996  211
   
A vector is created by typing the elements (numbers)
Inside square brackets [ ].
To create a ROW VECTOR, type a space or a comma
between the elements inside the square brackets.
NOTE: Matlab is not “picky” about how the data

is typed in. You can type spaces before and/or

after the sign “=“.

Between the elements you can have a space in

addition to the comma, or you can type more

than one space.


To create a COLUMN VECTOR, type a left bracket [, and
Then enter the elements with a semicolon between
them, or press <ENTER> after each element. Type a
right bracket ] after the last element.
CREATING VECTORS WITH CONSTANT SPACING

Two common methods:

- Specify first term : step size : last term

- linspace (first term, last term, number of terms)


Command Description
cat(n,A,B,C,…) Creates a new array by concatenating the arrays A,B,C
and so on along the dimension n.
find(x) Computes an array containing the indices of the nonzero
elements of the array x.
[u,v,w]=find(A) Computes the arrays u and v, containing the row and
column indices of the nonzero elements of the matrix A,
and the array w, containing the values of the nonzero
elements. The array w may be omitted.
length (A) Computes either the number of elements of A if A is a
vector or the largest value of m or n if A is an mxn
matrix.
linspace(a,b,n) Creates a row vector of n regularly spaced values
between a and b.
logspace (a,b,n) Creates a row vector of n logarithmically spaced values
between a and b.
Command Description
max(A) Returns the algebraically largest element in A if A is a
vector. Returns a row vector containing the largest
elements in each column if A is a matrix. If any of the
elements are complex, max(A) returns the elements that
have the largest magnitudes.
Similar to max(A) but stores the maximum values in the
[x,k]=max(A) row vector x and their indices in the row vector k.
min(A) Same as max(A) but returns minimum values.
[x,k]=min(A) Same as [x,k] = max(A) but returns minimum values.
size(A) Returns a row vector [m,n] containing the sizes of the
mxn array A.
sort(A) Sorts each column of the array A in ascending order and
returns an array the same size as A.
sum(A) Sums the elements in each column of the array A and
returns a row vector containing the sums.
Command Description Form Example
+ Scalar-array addition A+b [6,3]+2=[8,5]
- Scalar-array subtraction A-b [8,3]-5=[3,-2]
+ Array addition A+B [6,5]+[4,8]=[10,13]
- Array subtraction A-B [6,5]-[4,8]=[2,-3]
.* Array multiplication A.*B [3,5].*[4,8]=[12,40]
./ Array right division A./B [2,5]./[4,8]=[2/4,5/8]
.\ Array left division A.\B [2,5].\[4,8]=[2\4,5\8]
.^ Array exponentiation A.^B [3,5].^2=[3^2,5^2]
2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
Multiplication of Vectors
u=[u1 u2 u3], v=[v1 v2 v3]
u.v=u1v1 +u2v2 +u3v3
Example:
Vector-matrix multiplication

u11 u12 u13   v11  Example:

u = u21 u22 u23  v = v21 


u31 u32 u33  v31 
 u11v11 + u12v21 + u13v31 

u.v = u21v11 + u22v21 + u23v31  
u31v11 + u32v21 + u33v31 
Matrix-matrix multiplication
Example:

u11 u12 
   v11 v12 
u = u21 u22  v= 
u31 u32   v21 v22 

 u11v11 + u12v21 u11v12 + u12v22 


u.v = u21v11 + u22v21 u21v12 + u22v22 
u31v11 + u32v21 u31v12 + u32v22 
Special matrices
Null matrix 0: 0A=A0=0
Identity (Unity) matrix I: IA=AI=A
Command Description
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
ones(size(A)) Creates an array of ones the same size as the array A
zeros(n) Creates an nxn matrix of zeros
zeros(m,n) Creates an mxn array of zeros
zeros(size(A)) Creates an array of zeros the same size as the array A
Ex:
10 0  x  2
Plot the function 
f (x ) =  0 2  x  5
− 3 5  x  7

f (x ) = a1 x n + a2 x n −1 + a3 x n − 2 + ... + an −1 x 2 + an x + an +1
f(x): the function of x, degree(order): n
ai (i=1,1,….,n+1): polynomial’s coefficients
→ using row vector to describe a polynomial
→ f(x) can be described as follows:
a , a , a ,..., a
1 2 3 n −1 , an , an +1 

Ex: [4,-8,7,-5] represents for: 4 x − 8x + 7 x − 5


3 2
➢To find polynomial roots → roots (a)
(a): array contaning the polynomialcoefficients.
➢To compute the coefficients of the polynomial
whose roots are specified by the array (a)
→ poly(a)
Given 2 polynomials
f (x ) = 9 x 3 − 5 x 2 + 3x + 7  f = 9,−5,3,7
g ( x) = 6 x 2 − x + 2  g = 6,−1,2
Given 2 polynomials

f (x ) = 9 x 3 − 5 x 2 + 3x + 7  f = 9,−5,3,7
g ( x) = 6 x 2 − x + 2  g = 6,−1,2
Plotting Polynomials
Polyval(a,x): evaluates a polynomial at specified
values of its independent variable x, which can be
a matrix or a vector.
( )
Ex: f x = 9 x − 5 x + 3x + 7
3 2

Find the value of f(x) at x=0,2,4,…,10


Plotting Polynomials
Plot the polynomial f(x) for 0x10
f ( x ) = 9 x 3 − 5 x 2 + 3x + 7
PRACTICE 1
Type this matrix in Matlab and use Matlab to answer the following
questions:
 3 7 − 4 12
− 5 9 10 2 
A= 
 6 13 8 11
 
 15 5 4 1 

1. Create a vector v consisting of the elements in the second column of A.


2. Create a vector w consisting of the elements in the second row of A.
3. Create a 4x3 array B consisting of all elements in the second through fourth
columns of A.
4. Create a 4x3 array B consisting of all elements in the second through fourth
rows of A.
5. Create a 2x3 array D consisting of all elements in the first two rows and the
last three columns of A.
PRACTICE 2
Given the matrix:

 3 7 − 4 12
− 5 9 10 2 
A= 
 6 13 8 11
 
 15 5 4 1 

1. Find the minimum and maximum values in each column, row.


2. Sort each column and store the result in an array B.
3. Sort each row and store the result in an array C.
4. Add each column and store the result in an array D.
5. Add each row and store the result in an array E.
PRACTICE 3
Given the matrices:

− 7 16   6 − 5  − 3 − 9
A=  B=  C= 
 4 9  12 − 2   6 8 

1. Find A+B+C
2. Find A-B+C
3. Find A.B
4. Find D=A-B.C
5. Find D raised to the third power element-by-element
PRACTICE 4

Use Matlab to confirm that:

(20 x 3
)( )
− 7 x 2 + 5 x + 10 4 x 2 + 12 x − 3 =
80 x 5 + 212 x 4 − 124 x 3 + 121 x 2 + 105 x − 30

Plot the polynomial:


y = x 3 + 13 x 2 + 52 x + 6

over the range -7x1


PRACTICE 5
PRACTICE 6
PRACTICE 7
PRACTICE 8
PRACTICE 9

You might also like