0% found this document useful (0 votes)
3 views5 pages

Da Lab4

The document contains an R script for performing various matrix operations including addition, subtraction, multiplication, division, transpose, and inverse. It prompts the user to input the dimensions and elements of two matrices, A and B, and then executes the specified operations while checking for conditions like invertibility. The results of each operation are printed to the console.

Uploaded by

iamkirito108
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)
3 views5 pages

Da Lab4

The document contains an R script for performing various matrix operations including addition, subtraction, multiplication, division, transpose, and inverse. It prompts the user to input the dimensions and elements of two matrices, A and B, and then executes the specified operations while checking for conditions like invertibility. The results of each operation are printed to the console.

Uploaded by

iamkirito108
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/ 5

Name – Sneha Veerbhan

CSIT 6C1

2200290110168

DA LAB- 4
Exp4:

To get input matrix from user and perform Metrix add, subtract, multiplication, inverse
transpose and division operator in R

Code:

cat("Enter number of rows: ")

rows <- as.integer(readline())

cat("Enter number of columns: ")

cols <- as.integer(readline())

cat("Enter elements of Matrix A (row-wise, separated by space):\n")

elements_A <- as.numeric(strsplit(readline(), " ")[[1]])

A <- matrix(elements_A, nrow = rows, ncol = cols, byrow = TRUE)

cat("Enter elements of Matrix B (row-wise, separated by space):\n")

elements_B <- as.numeric(strsplit(readline(), " ")[[1]])

B <- matrix(elements_B, nrow = rows, ncol = cols, byrow = TRUE)

cat("\nMatrix A:\n")

print(A)
cat("\nMatrix B:\n")

print(B)

#Addition

add_result <- A + B

cat("\nMatrix Addition (A + B):\n")

print(add_result)

#Subtraction

sub_result <- A - B

cat("\nMatrix Subtraction (A - B):\n")

print(sub_result)

#Multiplication

mul_result <- A %*% B

cat("\nMatrix Multiplication (A %*% B):\n")

print(mul_result)

# Division

if (det(B) != 0) { # Check if B is invertible

div_result <- A %*% solve(B)

cat("\nMatrix Division (A * Inverse(B)):\n")

print(div_result)

} else {

cat("\nMatrix B is not invertible. Division not possible.\n")

#Transpose of Matrices
cat("\nTranspose of Matrix A:\n")

print(t(A))

cat("\nTranspose of Matrix B:\n")

print(t(B))

#Inverse of Matrices

if (nrow(A) == ncol(A) && det(A) != 0) {

cat("\nInverse of Matrix A:\n")

print(solve(A))

} else {

cat("\nMatrix A is not invertible.\n")

if (nrow(B) == ncol(B) && det(B) != 0) {

cat("\nInverse of Matrix B:\n")

print(solve(B))

} else {

cat("\nMatrix B is not invertible.\n")

You might also like