Computer Programming (Matlab) : Vectors, MATRIX

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

COMPUTER PROGRAMMING

(MATLAB)
Vectors, MATRIX
Computer Engineering
Nile University of Nigeria

28/03/22 07:14 PM 1
Contents

oVectors
oVector operations
oPlotting vectors
oMatrix
oMatric generation
oMatrix operations

28/03/22 07:14 PM 2
MATRIX
o The basic object that MATLAB deals with is ……..
a matrix.
o -------------- are the basic elements of the MATLAB environment.
Matrices
o A matrix is a two-dimensional array consisting of m --- and n ----.
row, columns
o Special cases are column vectors (n = 1) and row vectors (m = 1).

28/03/22 07:14 PM 3
MATRIX
MATLAB treats all variables as
matrices.
For our purposes a matrix can be thought of as an array, in
fact, that is how it is stored.

Vectors are special forms of matrices and contain only one


row OR one column.

Scalars are matrices with only one row AND one column
28/03/22 07:14 PM 4
Entering vector
o Matrices: are fundamental to MATLAB. so, we need to become familiar with
matrix generation and manipulation. Matrices can be generated in several ways.
o A Vectors is a special case of matrix. The elements of vectors in MATLAB are
enclosed by square brackets and are separated by spaces or by commas. For
example, to enter a row vector, x, type
>> x = [1 5 8 11 14]
Column vector y
>> y = [1;5;8;11;14]
Row vector is converted to a column vector using transpose operator
It is denoted by an (’).
>> x = y’

28/03/22 07:14 PM 5
Entering vector cont..
• Vectors are stored in MATLAB as one-dimensional arrays.
• Example: >> x = [1 3 0 5 -2]
oExample: >> y = pi*x
oThe vector y is obtained from the vector x by; x *
oTo access the first element of vector y, we type y(1)
oThe Colon (:) operator denote to, end signifies the last
element. It occurs in several different forms.

28/03/22 07:14 PM 6
Entering vector cont..
oIn order to obtain the length of a vector, we use the
MATLAB command length as follow
o>> length(y)
oTo find the minimum value and maximum value, use the
MATLAB commands min and max as follows;
omin(y)
omax(y)
oTASK 1: Add the total sum of the values of the elements of
vector y.

28/03/22 07:14 PM 7
Entering vector cont..
o The Colon (:) operator denote to, end signifies the last element. It occurs in
several different forms.
o Matrices or vectors that are too large to enter, not easy to deal with
o Suppose we want to enter v consisting of points(0, 0.1,0.2,0.3,….,5).
o We can just use v = 0:0.1:5;. How many row vector does vector v have??
o Task 2: Access the first three elements of vector x
o Task 3: All elements from the third through the last elements of vector x
o Task 4: type x(:) and x(1:end) and see what type of vector they produce

28/03/22 07:14 PM 8
Entering vector cont..
o Another way to construct vectors in MATLAB is to use the linspace
command (using parentheses) as follows:
o >> b = linspace(0, 5, 10)

o Create a vector of 100 evenly spaced points in the interval[-10,10]

o logspace
o Create a vector of 50 logarithmically spaced points in the interval
[10^a, 10^b]
o Tow vectors can be joined to form a new vector

28/03/22 07:14 PM 9
Entering vector cont..
oTow vectors can be joined to form a new vector
oExample: >> a = [1 3 5]. >> b = [5 8]. >> c = [a b]
oWe can add or subtract two vectors of the same length
oExample:>> w = [0.1 0.4 7 11] , >> v = [1 2 3 4]
oGet w + v and w – v
oWhat happens when you try to add two vectors of different
lengths?
oHow about multiplication and division?

28/03/22 07:14 PM 10
Entering vector cont..
oThere are other simpler operations that can be performed on
vectors. The operations of scalar addition and scalar
subtraction can be performed as in the following example
>> s = [1 2 -4 3 8 1]
>> s + 5;
>> s – 2;
>> s * 6;
>> s/2

28/03/22 07:14 PM 11
Vectors with the MATLAB Symbolic Math Toolbox
oSymbolic vectors are vectors whose elements are handled
algebraically without numerical computations.

oIn the following example, we define a symbolic vector a of


three elements using the MATLAB command syms:

28/03/22 07:14 PM 12
Vectors with the MATLAB Symbolic Math Toolbox
o>> syms x y z
o>> a = [x y z]
o>> b = 2 + a

oExtract the 3rd element of vector b

oPerform addition of two vectors a and b to get c

oPerform 2*a – 3*b/5 to obtain new symbolic vector d

oObtain the dot product of vector a and b

28/03/22 07:14 PM 13
Entering a matrix
• Matrices are stored as two-dimensional arrays in MATLAB. A matrix
is a collection of numbers and/or scalars arranged in rows and
columns. For example here is a matrix of numbers with three rows and
three columns
o To type a matrix into MATLAB ……
o >> A = [1 2 3; 4 5 6; 7 8 9]

o A=
• In the above example, the matrix A is Square because the number of
columns and the number of rows are equal

28/03/22 07:14 PM 14
Scalar Multiplication
A=

2 x =

28/03/22 07:14 PM 15
Matrix by Hand Number of sells
in 4 days
Multiplying a Matrix by
Another Matrix Mon Tues Wed Thu
o We need to do the dot product Meat pie 13 9 7 15
of rows and columns
o Example: Local shop sells 3 Samosa 8 7 4 6
types of snacks Shawarma 6 4 0 3
o Meat pie cost ₦30 each
o Samosa cost ₦40 each
o Shawarma cost ₦20 each mxn X nxp mxp

28/03/22 07:14 PM 16
Matrix …..
o>> A(3, 2)
oA new matrix of the same size may be generated
o>> B = 2*pi*x
oWe can extract the elements in the 1st and 2nd rows and the 2nd and 3rd
columns as follow
o>> B(1:2, 2:3)
oNumber of elements in a matrix is obtained by using the MATLAB
command numel
o>> numel(B)
oUse the sum, min, and max commands that we used earlier for vectors
oWe can also combine 2 or more matrices to obtain a new matrix just as we
did for vectors.

28/03/22 07:14 PM 17
Matrix
oWe can perform the operations of matrix addition and
matrix subtraction on two matrices of the same size

oExample: x = [0.2 -0.1; 1.3 0.0; 2.4 -1.5]

oy = [0.5 1.0; 1.2 1.3; -2.0 1.3]

ox+y. x-y

28/03/22 07:14 PM 18
Matrix
oMatrix multiplication is not defined for two matrices of the same
size (unless they are square matrices).

oWhat happens if you try to multiply two matrices of the same


size?

oHow can we do it?


oWe can multiply two matrices of the same size element-by-
element by adding the dot symbol before the multiplication
symbol as follows: x.*y. same thing applies to division
28/03/22 07:14 PM 19
Matrix
• Two matrices may be multiplied if the number of columns of the first
matrix equals the number of rows of the second matrix.
• Here is an example
• >> u = [1 3; -2 0; 1 5]
• >> v = [2 3 1 -2; 1 0 -1 2]
• >> u*v. multiply 3x2 matrix with 2x4 matrix.
Rule of matrix multiplication applies
• u*4
• 3-2*u/1.5
• w = [0 pi/2 pi; pi/2 pi 3*pi/2; pi 3*pi/2 2-pi]
• sin(w), tan(w) etc.
28/03/22 07:14 PM 20
Square matrix
oones, zeros and eye commands can be used to generate standard square
matrices

oExample: type
o>> ones(3)
o>> zeros(3)
o>> eye(3)

o The transpose of a matrix is generated in MATLAB using the prime symbol


oA=[1 3 2; 3 2 1; 5 1 7]
oA'
28/03/22 07:14 PM 21
Matrix indexing
oNeed 2 indices

oSingle element of a matrix are accessed as A(i , j), where


i>=1.

oElements of row i and column j of the matrix A is


denoted by A(i, j).

28/03/22 07:14 PM 22
Matrix indexing

oThe first index is the row number and the second index is the
column number. E.g. A(2,2) is an element of first second and
second column.

oCorrecting entry can be done through indexing. We substitute


A(2,2)= 5 by A(2,2) = 20.
Examine the result…..

28/03/22 07:14 PM 23
Deleting row or column
o To delete a row or column of a matrix, use the empty vector
operator, [ ].

>> A (3, :) = []
• First row of matrix A is now deleted. To restore the third row,
we use a technique for creating a matrix

>> A = [A(1,:);A(2,:);[7 8 9]] the matrix A will be restore to its


original form

28/03/22 07:14 PM 24
Extracting a Sub-Matrix
A portion of a matrix can be extracted and stored in a smaller matrix by
specifying the names of both matrices and the rows and columns to extract.
The syntax is:

sub_matrix = matrix ( r1 : rn , c1 : cn ) ;
sub_matrix = matrix ( r1 : rn , : ) ;
sub_matrix = matrix ( : , c1 : cn ) ;
sub_matrix = matrix ( r1 : dr : rn , c1 : dc : cn ) ;

where r1 and rn specify the beginning and ending rows


and c1 and cn specify the beginning and ending columns to be extracted to
make the new matrix.
The terms dr and dc define spacing different than one.
Plotting vectors
x = 0:.1:2*pi;

y = sin(x);

plot(x,y)

The first line uses the colon operator to generate a vector x of


numbers running between 0 and 2π with increment 0.1.
28/03/22 07:14 PM 26
Plotting vectors
The second line calculates the sine of this array of numbers,
and calls the result y.

The third line produces a plot of y against x. Go ahead and


produce the plot.
You should get a separate window displaying this plot.

We have done in three lines of MATLAB.

28/03/22 07:14 PM 27
Saving Results
• We can save all our results for future reference.

• The command
diary ‘FileName’ saves all output to command window into
the FileName.txt file until this option is turned off by the
command diary off
Saving Results
• The following commands save & load the entire workspace into the file
'MyMatFile.mat'
• save 'MyMatFile'
• load 'MyMatFile'
• save 'x.mat' x % save a specific variable

• Saving in ASCII format:


• x = (-1:0.4:1)' ; y = sin(x*pi)
• var = [x y] % double-column
• save 'my_sin.dat' -ASCII -double var %Save in 16-digit ASCII format
Square Matrix
oPress Del to delete the character at the cursor

oBackspace to delete the character before the cursor

oCtrl and k to delete the end of the line Ecs to clear the entire
line

oPress ctrl c to clear a long computation without terminating


the session
28/03/22 07:14 PM 30
Questions

28/03/22 07:14 PM 31
Practice question
1. Given x = -5 + 9i and 6 – 2i, use MATLAB to show that x +y = 1 +
7i, x*y = -12 + 64i, and x/y = -1.2 + 1.1i
2.Use MATLAB to solve the following Sets of linear equations:
o 6x + 12y + 4z = 70
o 7x – 2y + 3z = 5
o 2x + 8y – 9z = 64
• You can use the left division operator (\) in MATLAB to solve sets of
linear algebraic equations

28/03/22 07:14 PM 32

You might also like