Fop 2.1
Fop 2.1
Fop 2.1
1
Student Name: Paramita Halder UID: 22MSM40017
Branch: M.Sc Data Science Section/Group: 22MSD-1/A
Semester: 2nd Date of Performance:- 03-10-2022
Subject Name: Fundamental of Programming Subject Code: 22SDP-607
Tasks to be performed
Write a program to implement matrix operations.
● Matrix Addition
● Matrix Subtraction
● Matrix Multiplication
● Matrix Division
Theory
Matrices in R
Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout.
They contain elements of the same atomic types. Though we can create a matrix containing only
characters or only logical values, they are not of much use. We use matrices containing numeric
elements to be used in mathematical calculations.
A Matrix is created using the matrix() function.
Syntax:
{
matrix(data, nrow, ncol, byrow, dimnames)
}
Following is the description of the parameters used −
● data is the input vector which becomes the data elements of the matrix.
● nrow is the number of rows to be created.
● ncol is the number of columns to be created.
● byrow is a logical clue. If TRUE then the input vector elements are arranged by row.
● dimname is the names assigned to the rows and columns
Operations on Matrices
❖ Matrix Addition
The addition of two same ordered matrices
and
yields a matrix
where every
element is the sum of corresponding elements of the input matrices.
Properties of Matrix Addition:
Commutative: B + C = C + B
Associative: For n number of matrices A + (B + C) = (A + B) + C
Order of the matrices involved must be the same.
❖Matrix Subtraction
The subtraction of two same ordered matrices
and
yields a matrix
where every
element is the difference of corresponding elements of the input matrices.
Properties of Matrix Subtraction:
Non-Commutative: B + C ≠C + B
Non-Associative: For n number of matrices A + (B + C) ≠ (A + B) + C
Order of the matrices involved must be the same
❖Matrix Multiplication
When a matrix is multiplied with another matrix, the element-wise multiplication of two matrices
take place. All the corresponding elements of both matrices will be multiplied under the condition
that both matrices will be of the same dimension.
If a matrix is multiplied with a vector then the vector will be promoted to either row or column
matrix to make two arguments conformable.
The Operator %*% is used for matrix multiplication satisfying the condition that the number of
columns in the first matrix is equal to the number of rows in the second. If matrix A[M, N] and
matrix B[N, Z] are multiplied then the resultant matrix will of dimension M*N.
Properties of Matrix Multiplication:
● Commutative: B * C = C * B
● Associative: For n number of matrices A * (B * C) = (A * B) * C
● Order of the matrices involved must be the same.
❖Matrix Division
The elements of div matrix are the division of the corresponding elements of B and C through
nested for loops. Using ‘/’ operator for matrix division: Similarly, the following R script uses the
in-built operator /.
Properties of Matrix Division:
● Non-Commutative: B / C ≠ C / B
● Non-Associative: For n number of matrices A / (B / C) ≠ (A / B) / C
● Order of the matrices involved must be the same.
Practical
❖Adding two Matrices
II. Then we opened a new worksheet and visualized the profit of states with more than 10,000 total
profits on the map of united states.
III. Then we opened another worksheet and plotted a side-by-side bar graph of Sales vs Profit for top 4
states in total sales.
IV. Then we opened another worksheet and plotted a side-by-side bar graph Sales vs Profit for top
9 states in total profit.
V. Then we created a new story by clicking on story -> new story and added the four worksheets in
it with appropriate captions as shown in the figures below.
Learning outcomes (What I have learnt):