Programs to Try using matrix in R - Assignment 7
Experiment 1: Basic Matrix Operations in R
Aim
To create matrices in R and perform basic matrix operations, indexing, slicing, and built-in matrix functions.
Problem Statement
Write an R program to create the following 3 × 4 matrix:
10 20 30 40 50 60 70 80 90 100 110 120
Perform the following operations:
A. Matrix Creation and Display
-
Create the matrix using
matrix(). - Display the matrix.
- Display the number of rows and columns.
- Display the dimensions of the matrix.
B. Matrix Indexing
- Display the element in the second row and third column.
- Display the complete first row.
- Display the complete fourth column.
- Display the element in the third row and first column.
C. Matrix Slicing
- Extract the first two rows.
- Extract the last two rows.
- Extract the first two columns.
- Extract the last two columns.
- Extract the 2 × 2 submatrix consisting of rows 2–3 and columns 2–3.
D. Matrix Functions
- Find the sum of all elements.
- Find the maximum element.
- Find the minimum element.
- Find the mean of all elements.
- Find the sum of each row.
- Find the sum of each column.
- Find the mean of each row.
- Find the mean of each column.
E. Matrix Manipulation
- Find the transpose of the matrix.
- Add 10 to every element of the matrix.
- Multiply every element by 2.
- Extract the diagonal elements from a suitable square submatrix
F.Additional Practice
- Display the elements in row 1, columns 2–4.
- Display rows 1 and 3 only.
- Display columns 1 and 3 only.
- Extract the following submatrix:
60 70 100 110
- Find the largest element in the second row.
- Find the smallest element in the third column.
- Find the sum of the first two rows.
- Find the average of the last two columns.
-
Replace the element at row 2, column 3 with
75. -
Replace the entire first row with
1, 2, 3, 4.
Experiment 2: Matrix Arithmetic and Advanced Matrix Operations in R
Aim
To write R programs to perform matrix arithmetic, transpose, diagonal extraction, determinant, inverse, and solution of simultaneous linear equations.
Problem Statement
Consider the following two matrices:
and
Write an R program to perform the following operations:
-
Create and display matrices
AandB. -
Find
A + B. -
Find
A - B. -
Perform element-wise multiplication of
AandB. -
Perform element-wise division of
AbyB. -
Perform matrix multiplication of
AandB. -
Find the transpose of
A. -
Find the determinant of
A. -
Find the inverse of
A. -
Verify the inverse by calculating
A %*% solve(A). -
Extract the diagonal elements of
A. - Calculate the sum of the diagonal elements (trace).
- Solve the following system of simultaneous linear equations using matrix operations:
-
Verify the obtained values of
xandyby substituting them back into the original equations. - Find the eigen values and eigen vectors of a 3x3 matrix
- Find the rank of a 3x3 matrix.
Experiment 3: Analysis of Student Performance Using Matrices
Aim
To use matrices in R for analyzing student performance and to apply matrix arithmetic, matrix multiplication, transpose, determinant, inverse, rank, and eigenvalue/eigenvector analysis to a real-world dataset.
Problem Statement
A college conducts three examinations for four students in three subjects:
- Mathematics
- Programming
- Data Structures
The marks obtained by the students are represented using a matrix.
Consider the following marks:
where each row represents a student and each column represents a subject.
Write an R program to perform the following analysis.
Part A – Basic Analysis
- Create the marks matrix with appropriate row and column names.
- Display the matrix.
- Display the marks obtained by the third student.
- Display the marks obtained by all students in Programming.
- Find the total marks obtained by each student.
- Find the average marks obtained by each student.
- Find the average mark in each subject.
- Find the highest mark in each subject.
- Identify the student with the highest total marks.
Part B – Matrix Operations
- Assume the three subjects have different importance and assign the following weights:
Calculate the weighted score of each student using matrix multiplication.
- Rank the students according to their weighted scores.
- Calculate the transpose of the marks matrix.
- Calculate:
and display the resulting matrix.
Part C – Advanced Matrix Analysis
Create the following 3 × 3 subject correlation-like matrix:
S <- matrix(c( 1.0, 0.6, 0.4, 0.6, 1.0, 0.7, 0.4, 0.7, 1.0 ), nrow = 3, byrow = TRUE)
-
Find the determinant of
S. -
Find the inverse of
S. -
Find the rank of
S. -
Find the eigenvalues of
S. -
Find the eigenvectors of
S. - Identify the largest eigenvalue.
- Display the eigenvector corresponding to the largest eigenvalue.
Comments
Post a Comment