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

  1. Create the matrix using matrix().
  2. Display the matrix.
  3. Display the number of rows and columns.
  4. Display the dimensions of the matrix.

B. Matrix Indexing

  1. Display the element in the second row and third column.
  2. Display the complete first row.
  3. Display the complete fourth column.
  4. Display the element in the third row and first column.

C. Matrix Slicing

  1. Extract the first two rows.
  2. Extract the last two rows.
  3. Extract the first two columns.
  4. Extract the last two columns.
  5. Extract the 2 × 2 submatrix consisting of rows 2–3 and columns 2–3.

D. Matrix Functions

  1. Find the sum of all elements.
  2. Find the maximum element.
  3. Find the minimum element.
  4. Find the mean of all elements.
  5. Find the sum of each row.
  6. Find the sum of each column.
  7. Find the mean of each row.
  8. Find the mean of each column.

E. Matrix Manipulation

  1. Find the transpose of the matrix.
  2. Add 10 to every element of the matrix.
  3. Multiply every element by 2.
  4. Extract the diagonal elements from a suitable square submatrix

F.Additional Practice

  1. Display the elements in row 1, columns 2–4.
  2. Display rows 1 and 3 only.
  3. Display columns 1 and 3 only.
  4. Extract the following submatrix:
        60  70
        100 110
  1. Find the largest element in the second row.
  2. Find the smallest element in the third column.
  3. Find the sum of the first two rows.
  4. Find the average of the last two columns.
  5. Replace the element at row 2, column 3 with 75.
  6. 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:

A=[4235]A = \begin{bmatrix} 4 & 2\\ 3 & 5 \end{bmatrix}

and

B=[1324]B = \begin{bmatrix} 1 & 3\\ 2 & 4 \end{bmatrix}

Write an R program to perform the following operations:

  1. Create and display matrices A and B.
  2. Find A + B.
  3. Find A - B.
  4. Perform element-wise multiplication of A and B.
  5. Perform element-wise division of A by B.
  6. Perform matrix multiplication of A and B.
  7. Find the transpose of A.
  8. Find the determinant of A.
  9. Find the inverse of A.
  10. Verify the inverse by calculating A %*% solve(A).
  11. Extract the diagonal elements of A.
  12. Calculate the sum of the diagonal elements (trace).
  13. Solve the following system of simultaneous linear equations using matrix operations:
4x+2y=104x + 2y = 10 3x+5y=133x + 5y = 13
  1. Verify the obtained values of x and y by substituting them back into the original equations.
  2. Find the eigen values and eigen vectors of a 3x3 matrix
  3. 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:

M=[807585657072908895706568]M = \begin{bmatrix} 80 & 75 & 85\\ 65 & 70 & 72\\ 90 & 88 & 95\\ 70 & 65 & 68 \end{bmatrix}

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

  1. Create the marks matrix with appropriate row and column names.
  2. Display the matrix.
  3. Display the marks obtained by the third student.
  4. Display the marks obtained by all students in Programming.
  5. Find the total marks obtained by each student.
  6. Find the average marks obtained by each student.
  7. Find the average mark in each subject.
  8. Find the highest mark in each subject.
  9. Identify the student with the highest total marks.

Part B – Matrix Operations

  1. Assume the three subjects have different importance and assign the following weights:
W=[0.30.40.3]W = \begin{bmatrix} 0.3\\ 0.4\\ 0.3 \end{bmatrix}

Calculate the weighted score of each student using matrix multiplication.

  1. Rank the students according to their weighted scores.
  2. Calculate the transpose of the marks matrix.
  3. Calculate:
MTMM^T M

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)
  1. Find the determinant of S.
  2. Find the inverse of S.
  3. Find the rank of S.
  4. Find the eigenvalues of S.
  5. Find the eigenvectors of S.
  6. Identify the largest eigenvalue.
  7. Display the eigenvector corresponding to the largest eigenvalue.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Programs to try using Functions in R - Assignment 3