Matlab Intro to Arrays Guide for Beginners.pdf

JesseGrant9 11 views 33 slides Mar 12, 2025
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

Matlab Guide


Slide Content

MATH 1194
Introduction to Mathematical Software III
- A Primer on MATLAB
Lecturer: Mr. Jagdesh Ramnanan
Department of Mathematics and Statistics
UWI, St. Augustine
Semester II - 2024/2025




2 - Arrays
Lecturer: Mr. Jagdesh Ramnanan 1 / 33

MATLAB Data Classes
https://www.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-
classes.html
Lecturer: Mr. Jagdesh Ramnanan 2 / 33

What is an Array?
An array is a set of elements arranged in rows & columns.
The elements can be numbers, letters or other objects.
Avectoris an array consisting of a single line of items.
Arow vectoris a horizontal line of items.
Acolumn vectoris a vertical line of items.
Thelengthof a vector is the number of elements it
contains.
An?????? × ??????matrixis an array that has??????rows and??????
columns.
Thesizeof a matrix is the dimensions of the matrix
(the number of rows and columns).
Thelengthof a matrix is the largest dimension of the
matrix (the larger of??????and??????).
Lecturer: Mr. Jagdesh Ramnanan 3 / 33

Examples of Arrays
Row vectorof length 5:[1 0
1
3
−1 0.5]
Column vectorof length 4:




t
h
i
s




AMatrix:



1 3 5 7 10
1 0 −1 2 3
2 4 6 8 13



Note that the size of this matrix is3 × 5
Its length is 5, since 5 is bigger than 3
Lecturer: Mr. Jagdesh Ramnanan 4 / 33

Utility Matrices in MATLAB
> >[ ]- returns an empty matrix
> >zeros(m,n)- returns an?????? × ??????matrix of zeros
> >ones(m,n)- returns an?????? × ??????matrix of ones
> >eye(m,n)- returns an?????? × ??????matrix with ones on the
leading diagonal and zeros everywhere else
Note:Omitting “,n“ gives an?????? × ??????square matrix.
Omitting “(m,n)“ gives a single number.
Lecturer: Mr. Jagdesh Ramnanan 5 / 33

Generating Random Numbers
> >rand(m,n)- returns an?????? × ??????matrix of random numbers
in the interval(0, 1)
> >randi(imax,m,n)- returns an?????? × ??????matrix of random
integers from 1 toimax
Note:Omitting “,n“ above gives an?????? × ??????square matrix.
Omitting “(m,n)“ gives a single number.
The numbers generated above are not truly random, since
they follow default built-in patterns.
Enter the command> >rng(’shuffle’)before executing
the above commands to randomise the pattern.
Lecturer: Mr. Jagdesh Ramnanan 6 / 33

Inputting Vectors
Arow vectorcan be entered in MATLAB by typing a list
of numbers, separated bycommas orspaces, inside square
brackets.
For example, the following produce the same row vector:
> >x=[-2 1 3 6 0 7]
> >x=[-2,1,3,6,0,7]
Acolumn vectorcan be entered in MATLAB by typing
a list of numbers, separated bysemicolons, inside square
brackets.
For example, the following produces a column vector:
> >y=[2;1;5;3;8]
The commands> >length(x)and> >size(x)return
the length and dimensions ofx, respectively.
Lecturer: Mr. Jagdesh Ramnanan 7 / 33

Extracting an Element of a Vector
Thenth entry in a vectorxcan be extracted by using the
command> >x(n)
nmust be a positive integer not exceeding the length of
the vector
> >x(1)returns the 1st entry in the vectorx
> >y(5)returns the 5th entry in the vectory
> >y(6)returns an error
The last entry inxcan be extracted using the command
> >x(end)
Lecturer: Mr. Jagdesh Ramnanan 8 / 33

Extracting Multiple Elements of a Vector
Multiple entries can be extracted simultaneously by
specifying the entries as a vector (inside square brackets).
Entries of a row vector are returned as a row vector, and
those of a column vector are returned as a column vector.
Examples:
> >x([1 2 5])returns the 1st, 2nd and 5th entries ofx
> >y([1 4 2])returns the 1st, 4th and 2nd entries ofy
in that order
Lecturer: Mr. Jagdesh Ramnanan 9 / 33

Inputting Vectors with an Increment of 1
There are more efficient ways of entering a row vector
whose entries increase/decrease by a fixed increment.
If the increment is 1, then use the command> >x:y
wherexandyare the starting and ending numbers,
respectively.
Examples:
> >a=0:5returns the vectora= [0 1 2 3 4 5 ]
> >b=3.5:5.5returns the vectorb= [3.5 4.5 5.5]
> >c=5:1returns an empty array
Lecturer: Mr. Jagdesh Ramnanan 10 / 33

Inputting Vectors with other Increments
Otherwise, use the command> >x:r:ywherexandyare
the starting and ending numbers, respectively, andris
the increment (any real number).
Examples:
> >d=1:3:16
> >e=-1:0.4:1
> >f=-3:4/3:1
> >g=8:–1:4(Note the negative increment)
Lecturer: Mr. Jagdesh Ramnanan 11 / 33

Note when using the Increment Method
Enter the following command:
> >h=0:0.3:1
This produces the vectorh= [0 0.3 0.6 0.9]
When the specified ending number cannot be obtained by
successively adding the given increment to the starting
number, MATLAB ends the vector with the last number
obtained in the interval.
Another example:
> >k=2.5:-1:0returns the vectork= [2.5 1.5 0.5]
Lecturer: Mr. Jagdesh Ramnanan 12 / 33

Inputting Vectors using the linspace Command
Enter the command> >linspace(x,y,n), wherexandy
are the starting and ending points, andnis the number
of points.
> >l=linspace(1,16,6)
> >m=linspace(1,16,5)
The linspace command always ends the vector at the
specified ending number. The increment is given by
??????−??????
??????−1
.
If the number of pointsnis not specified, MATLAB
creates 100 points by default.
> >p=linspace(1,16)
Lecturer: Mr. Jagdesh Ramnanan 13 / 33

Inputting Matrices
A matrix can be entered as follows (insquare brackets):
Enter the first row of the matrix, using spaces or
commas to separate the entries
Type a semicolon after the last element in the first row
Enter the second row of the matrix, again using spaces
or commas to separate the entries
Type a semicolon after the last element in the second
row
Continue in this fashion until all elements have been
entered
For example, the3 × 5matrix
A=



1 3 5 7 10
1 0 −1 2 3
2 4 6 8 13



can be entered using:
> >A=[1 3 5 7 10; 1 0 -1 2 3; 2 4 6 8 13]
Lecturer: Mr. Jagdesh Ramnanan 14 / 33

Dimensions of a Matrix
The command> >size(X)returns the dimensions ofX
(number of rows, followed by number of columns).
> >size(A)returns the array[3 5]
The command> >length(X)returns the length ofX,
which is the larger of the two dimensions of the matrix.
> >length(A)returns5
The command> >numel(X)returns the number of
elements contained inX.
> >numel(A)returns15
Lecturer: Mr. Jagdesh Ramnanan 15 / 33

Exercise 1
Input the following matrices into MATLAB:
B= [
1 3
2 7
] ,C= [
−1 2
1.5 6
] ,
D=





3??????
2
0.5 −2
1 0 −
1
3
−1 2cosh(3)ln(2)
10
−3
log(sin(1))

??????
4
− 9
1 2 3





Note:When entering a large matrix, it can be helpful to
use ellipses to separate the rows of the matrix.
Lecturer: Mr. Jagdesh Ramnanan 16 / 33

Operations on Matrices - Addition/Subtraction
Two matrices of thesame sizecan be added together or
subtracted (element-wise).
> >B+C
> >B-C
> >A+Breturns an error since these have different sizes
A scalar can be added to or subtracted from each element
of a matrix.
> >B+3
> >C-pi
Lecturer: Mr. Jagdesh Ramnanan 17 / 33

Operations on Matrices - Multiplication
IfXis an?????? × ??????array andYis an?????? × ??????array, then the
command> >X*Yreturns the?????? × ??????array obtained
under normal matrix multiplication.
> >E=B*C
> >D*A
> >A*Areturns an error becauseAis not a square
matrix
Lecturer: Mr. Jagdesh Ramnanan 18 / 33

Operations on Matrices - Element-wise
Multiplication
IfXandYare arrays of thesamesize, then> >X.*Y
returns the array obtained by multiplying corresponding
elements of both arrays.
> >F=B.*C
> >G=A.*A
> >D.*Areturns an error since these have different sizes
Note:Be careful, because in general,X*YandX.*Y
produce different answers.
Lecturer: Mr. Jagdesh Ramnanan 19 / 33

Operations on Matrices - Element-wise Division
In Mathematics, while matrix multiplication is defined,
the the division of one matrix by another is not defined.
Therefore, we do not have any “normal matrix division”.
However, we can define element-wise matrix division.
IfXandYare arrays of thesamesize, then> >X./Y
returns the array obtained by dividing the elements ofX
by the corresponding elements ofY.
> >B./C
> >A./A
> >D./Areturns an error since they have different sizes
Lecturer: Mr. Jagdesh Ramnanan 20 / 33

Operations on Matrices - Exponentiation
IfXis asquarematrix andnis a positive integer, then
> >X^nreturns the matrix obtained by multiplyingXby
itselfntimes.
> >H=B^2
> >B^3
IfXis an array andris a real number, then> >X.^r
returns the array whose elements are those obtained by
raising the corresponding elements ofXto the powerr.
> >K=A.^2
> >A.^3
> >A.^0.5
Lecturer: Mr. Jagdesh Ramnanan 21 / 33

Operations on Matrices - Exponentiation
IfXis an array, then
> >sqrt(X)returns the array containing the square
root of each of the corresponding elements ofX.
> >exp(X)returns the array containing the natural
number?????? = 2.718...raised to the power of each of the
corresponding elements ofX.
> >L=sqrt(C)
> >M=exp(B)
Note:The commandssqrt(X)andX.^0.5return the
same answer.
Lecturer: Mr. Jagdesh Ramnanan 22 / 33

Operations on Matrices - Transposition
Thetransposeof an array is obtained by interchanging
each row and corresponding column.
When a row vector is transposed, it becomes a column
vector, and vice versa.
The transpose of an arrayXis denotedX
??????
In MATLAB, arrays are transposed using a single inverted
comma’
> >P=A’
Lecturer: Mr. Jagdesh Ramnanan 23 / 33

Extracting Information from Arrays (Indexing)
The command> >X(n)returns thenth entry inX.
Note that entries are counted vertically (along the first
column, then second column, and so on).
> >A(5)
The command> >X(i,j)returns the element stored in
rowi, columnjofX.
> >A(3,4)returns the element of A in row 3, column 4
The command> >X(i,:)returns all elements stored in
rowi.
> >A(3,:)returns all elements of A in row 3
The command> >X(:,j)returns all elements stored in
columnj.
> >A(:,4)returns all elements of A in column 4
Lecturer: Mr. Jagdesh Ramnanan 24 / 33

Extracting Information from Arrays (Indexing)
Multiple elements can be specified using vectors in the
row or column index.
> >A(2,3:5)returns the elements stored in row 2,
columns 3 to 5
> >A(:,[1 5 2])returns all the elements stored in
columns 1, 5 and 2 (in that order)
If we need to specify multiple rows or columns of a matrix,
we can first put these row/column numbers into a vector.
> >x=[1 5 2]; A(:,x)
Another example:
> >N=[1:6;7:12;13:18;19:24;25:30];
> >x=[2,4,3]; y=[1,2,6,4]; N(x,y)
Lecturer: Mr. Jagdesh Ramnanan 25 / 33

Manipulating Matrices
The value stored in a position in a matrix can be changed
by calling the entry and assigning it a new value.
> >N(2,3)=-1
Multiple values can be changed simultaneously.
> >N(5,:)=0
> >N([2,4,3],[1,2,6,4])=NaN
Values from one matrix can be replaced with values from
another.
> >P(5,3)=D(1,2)
> >N(4,1:2)=B(1,:)
Lecturer: Mr. Jagdesh Ramnanan 26 / 33

Manipulating Matrices
A row vectorrof length??????can be appended (added) to
an?????? × ??????matrixAusing the command> >[A;r]
A column vectorcof length??????can be appended (added)
to an?????? × ??????matrixAusing the command> >[A c]
Multiple rows or columns can be appended
simultaneously, once the new matrix is well-defined.
> >A=ones(3,4); r=zeros(1,4); c=2*ones(3,1);
> >X=[A; r]
> >Y=2*[A c]
> >Z=[X [3;3;3;3]; Y]
Lecturer: Mr. Jagdesh Ramnanan 27 / 33

Manipulating Matrices
The entries of a row or column of a matrix can be deleted
by equating it to the empty array[ ].
> >A(3,:)=[ ]deletes the 3rd row
> >A(:,1)=[ ]deletes the 1st column
Multiple rows (or columns) can be deleted simultaneously.
> >rng(’shuffle’); A=randi(9,10);
> >A([2 3 5 7],:)=[ ]deletes the rows 2, 3, 5 and 7
> >A(:,[1 2 4 8])=[ ]deletes columns 1, 2, 4 and 8
Lecturer: Mr. Jagdesh Ramnanan 28 / 33

Linear Algebra with MATLAB
Given a square matrixA, we can do the following in
MATLAB:
Compute the determinant ofA, denoted by|A|, using
the command> >det(A)
Find the inverse of the matrix (if it exists), denoted by
A
−1
, using the command> >inv(A)
Solve the system of equationsAx=busing the
command> >x=A\b
Lecturer: Mr. Jagdesh Ramnanan 29 / 33

Example
Consider the system of equations



1 0 2
3 2 1
4 −1 3



x=



3
−1
2



> >A=[1 0 2; 3 2 1; 4 -1 3]; b=[3;-1;2];
> >det(A)
> >inv(A)
> >x=A\b
Lecturer: Mr. Jagdesh Ramnanan 30 / 33

Exercise 2
Using an appropriate command,
create a row vectorxcontaining 8 equidistant points
from 1 to 13.
create a row vectorycontaining points from 1 to 13 in
increments of 3.
delete the first element and the last element ofx.
create a row vectorzby appending the number 15 at the
end of the vectory.
create a row vectoraby dividing the elements ofzby the
corresponding elements ofx.
create a2 × 3matrixAwhose first row consists of the
first 3 elements ofaand whose second row consists of
the last 3 elements ofa.
create a3 × 2matrixBby transposing the elements ofA.
Lecturer: Mr. Jagdesh Ramnanan 31 / 33

Exercise 3
Create the following matrix in MATLAB:
A=



1 0 2 3 4 −1
7 6 2 5 1 0
0 3 2 −2 −1 −1



Using an appropriate MATLAB command, together with
the matrixAabove, create the following matrix by
concatenating 4 suitable submatrices:
B=




1 0 111 111
7 6 111 111
0 0 2 2
0 0 5 −2




.
Lecturer: Mr. Jagdesh Ramnanan 32 / 33

Exercise 4
Given that
A=




1 0 0 2
3 2 1 0
4 −1 3 2
0 1 2 3




,b=




2
−1
3
1




use appropriate MATLAB commands to:
Find the determinant ofA.
Find the inverse ofA.
Find theexactsolutionxof the system of linear equations
Ax=b.
[Hint: The exact solution involves fractions.]
Lecturer: Mr. Jagdesh Ramnanan 33 / 33
Tags