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

Lecture 2 Vectors Metrices

Uploaded by

silentknight9715
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)
12 views

Lecture 2 Vectors Metrices

Uploaded by

silentknight9715
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/ 45

SCIENTIFIC COMPUTING USING

APPROPRIATE SOFTWARE
AMAT 21262

VECTORS AND MATRICES

Department of Mathematics
UoK
16.05.2024
OUTLINE

Creating Row Vectors, Column Vectors, and Matrices.

Accessing /Modifying Vector and Matrix Elements.

Operations on Vectors and Matrices

Matrix Concatenation
SCALAR, VECTOR (RAW / COLUMN), MATRIX

• Scalar, is a physical quantity that is completely described by its magnitude.


• A vector (One-dimensional array) is a list of scalar values arranged in a row
or column.
• A Metrix (two-dimensional array) is a list of scalar values arranged in a row
or column.
HOW TO CREATE VECTORS IN MATLAB?
Raw Vectors

A row vector can be created in many ways. First, the most direct way is putting the
desired values in a square bracket.
HOW TO CREATE VECTORS IN MATLAB?

For the regularly spaced vectors, the colon operator can be used to iterate through the
values. The syntax is

row_vec= First: Step: Last


HOW TO CREATE VECTORS IN MATLAB?

The third way of creating a row vector is using function linspace. The syntax is
linspace(x,y,n). This creates a vector with 𝑛 values in the inclusive range
from 𝑥 to 𝑦.
HOW TO CREATE VECTORS IN MATLAB?

Column Vectors
HOW TO CREATE METRIX IN MATLAB?

Suppose we want to create

1 2 3
A=
4 5 6
HOW TO CREATE METRIX IN MATLAB?

3 6 9
Let’s create 𝐵 =
2 6 10
Creating Vectors and Matrices in MATLAB using built-in
functions(Special Matrices)

• To create a 2 by 2 identity matrix 𝑨


• To create a 2 by 1 vector x with ones :

• To create a 3 by 4 vector y with zeros : • To create a diagonal matrix with elements in


𝒂 vector.
Random Values Matrices
Matrices can also be created from random values using rand and randi built-in functions.
To create an M by N matrix of random integers, each in the range from 0 to x. We can use randi
function.
Vector and Matrix Dimensions
• The length() function returns the number of element in a vector.

• The size() function returns the number of rows and columns of a matrix.

• length() can be applied to a matrix as well.


numel() function returns the total number of elements in any array (vector or matrix):
Accessing/Modifying Vector and Matrix Elements.

• Suppose that the vector v is defined as

v = [16 5 9 4 2 11 7 14]

• To extract the third element of v, v(3)

ans =9

• To extract the first, fifth, and sixth elements of v, v([1 5 6])

ans =16 2 11
Accessing/Modifying Vector and Matrix Elements cont.

v = [16 5 9 4 2 11 7 14]
• To extract the third through the seventh elements v(3:7)

ans =

9 4 2 11 7

• To extract the last element v(end)

ans =

14
Accessing/Modifying Vector and Matrix Elements cont.

Now consider the matrix A

A=

1 2 3 4
5 6 7 8
9 10 11 12

• To extract the element in row 2, column 4


A(2,4)

ans =
8

• To extract the third row of A


A(3,:)

ans =

9 10 11 12
Accessing/Modifying Vector and Matrix Elements cont.
• To extract last column
A(:,end) 1 2 3 4
5 6 7 8
ans = 9 10 11 12
4
8
12

• To extract all the elements of A that are greater than 8.


A(A>8)

ans =

9
10
11
12
Vector and Matrix Manipulation

Let’s consider the following matrix


A=

1 2 3 4
5 6 7 8
9 10 11 12

• To delete the second column of the vector A


A(:,2)=[]
A=

1 3 4
5 7 8
9 11 12
Vector and Matrix Manipulation Cont.

Let’s consider the following matrix B


B=

1 1 2
3 4 7
8 2 4

• To delete the first row of B:

B(1,:)=[]

B=
3 4 7
8 2 4
Vector and Matrix Manipulation Cont.
Let’s consider the following matrix C
C=

1 2 3
4 5 6

• To add a row that contains elements 7 8 9 at the end of the matrix C (or to append a row at
the end)
B=

7 8 9

C=[C;B]

C=

1 2 3
4 5 6
7 8 9
Changing Dimensions of Matrices

The following buit-in functions change the dimensions


or the configuration of the matrices.

reshape()
fliplr()
flipud()
rot()
reshape()-changes dimension of the matrix
fliplr()-flip the matrix from left to right
flipud()-flip the matrix from up to down
rot90()-rotates the matrix counter clockwise 90 degrees.
COMPUTATIONS ON VECTORS AND
MATRICES
Adding/ Subtracting Vectors
Multiplying Vectors

1× 𝑛 row vector can be multiplied 𝑛 × 1 column vector


with 𝑛 × 1 column vector can be multiplied with 1× 𝑛 row vector
Elementwise-Multiplication of Vectors.

This takes two Vectors of the same


dimensions and produces
another Vectors of the same
dimension as the operands, where
each element ( 𝑖) is
the multiplication of elements (𝒊)
in each vector.
Elementwise-Division of Vectors.
This takes two Vectors of the same
dimensions and produces
another Vectors of the same
dimension as the operands, where
each element ( 𝑖) is
the division of elements (𝒊) in each
vector.
Adding/ Subtracting Matrices
Multiplying Matrices
Inverse and Determinant of Matrices
Left and Right Division of Matrices

Left Division of Two Matrices A


and B : A/B
That is same as A* inv(B)

Right Division of Two Matrices


A and B : A\B
That is same as inv(A)*(B)
Elementwise-Multiplication of Matrices.

This takes two matrices of the same


dimensions and produces another matrix of the
same dimension as the operands, where
each element ( 𝑖, 𝑗 ) is the product of elements
(𝒊, 𝒋) in each matrix.
Elementwise-Division of Matrices.

This takes two matrices of the same


dimensions and produces
another matrix of the same
dimension as the operands, where
each element ( 𝑖, 𝑗 ) is
the divison of elements (𝒊, 𝒋) in each
matrix.
Elementwise-Power of a Matrix

This produces another matrix of the


same dimension as the operands,
where each element ( 𝑖, 𝑗 ) is
the power of elements (𝒊, 𝒋).
Transpose of Matrices.
Power of Matrices.
Matrix vector Product
Matrix Concatenation- Matrix can be created by appending
some matrices.
These concepts can be applied:
• For plotting graphs in MATLAB

• For solving a system of equations.

• etc,

2𝑥 + 3𝑦 + 4𝑧 = 5
−3𝑥 + 5𝑦 + 3𝑧 = 9
8𝑥 + 4𝑦 + 2𝑧 = −4

𝒙 -3 -2 -1 0 1 2 3

𝑦 = 𝑥2 9 4 1 0 1 4 9

You might also like