Professional Documents
Culture Documents
Computer Programming (Matlab) : Vectors, MATRIX
Computer Programming (Matlab) : Vectors, MATRIX
Computer Programming (Matlab) : Vectors, MATRIX
(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.
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 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.
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
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
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).
oExample: type
o>> ones(3)
o>> zeros(3)
o>> eye(3)
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.
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
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 ) ;
y = sin(x);
plot(x,y)
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
oCtrl and k to delete the end of the line Ecs to clear the entire
line
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