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

Experiment_3(DA)

This document outlines an R script for performing various matrix operations including addition, subtraction, multiplication, inverse, transpose, and division. It includes a function to get matrix input from the user and checks for conditions such as matrix dimensions and singularity before performing operations. The script is designed to handle both square and non-square matrices while providing appropriate messages for invalid operations.

Uploaded by

studybuddy060903
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)
2 views

Experiment_3(DA)

This document outlines an R script for performing various matrix operations including addition, subtraction, multiplication, inverse, transpose, and division. It includes a function to get matrix input from the user and checks for conditions such as matrix dimensions and singularity before performing operations. The script is designed to handle both square and non-square matrices while providing appropriate messages for invalid operations.

Uploaded by

studybuddy060903
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/ 6

EXPERIMENT-3

AIM: To get the input matrix from user and perform Matrix addition, subtraction, multiplication,
inverse transpose and division operations using vector concept in R.

CODE:

# Function to get matrix input from the user

get_matrix <- function(rows, cols, matrix_name) {

cat("Enter elements for", matrix_name, ":\n")

mat <- matrix(nrow = rows, ncol = cols)

for (i in 1:rows) {

for (j in 1:cols) {

mat[i, j] <- as.numeric(readline(paste("Enter element [", i, ",", j, "]: ")))

return(mat)

# Function to perform matrix operations

matrix_operations <- function() {

# Get dimensions of the matrices

rows <- as.numeric(readline("Enter the number of rows: "))

cols <- as.numeric(readline("Enter the number of columns: "))

# Get matrices from the user

mat1 <- get_matrix(rows, cols, "Matrix 1")


mat2 <- get_matrix(rows, cols, "Matrix 2")

# Matrix Addition

addition <- mat1 + mat2

cat("\nMatrix Addition:\n")

print(addition)

# Matrix Subtraction

subtraction <- mat1 - mat2

cat("\nMatrix Subtraction:\n")

print(subtraction)

# Matrix Multiplication

if (cols == rows) {

multiplication <- mat1 %*% mat2

cat("\nMatrix Multiplication:\n")

print(multiplication)

} else {

cat("\nMatrix Multiplication is not possible (columns of Matrix 1 must equal rows of Matrix
2).\n")

# Matrix Inverse (only for square matrices)

if (rows == cols) {

# Check if the matrix is non-singular before computing the inverse

if (det(mat1) != 0) {

inverse_mat1 <- solve(mat1)


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

print(inverse_mat1)

} else {

cat("\nMatrix 1 is singular and does not have an inverse.\n")

if (det(mat2) != 0) {

inverse_mat2 <- solve(mat2)

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

print(inverse_mat2)

} else {

cat("\nMatrix 2 is singular and does not have an inverse.\n")

} else {

cat("\nInverse is only possible for square matrices.\n")

# Matrix Transpose

transpose_mat1 <- t(mat1)

transpose_mat2 <- t(mat2)

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

print(transpose_mat1)

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


print(transpose_mat2)

# Matrix Division (multiplying by the inverse)

if (rows == cols) {

if (det(mat2) != 0) {

division <- mat1 %*% solve(mat2)

cat("\nMatrix Division (Matrix 1 / Matrix 2):\n")

print(division)

} else {

cat("\nMatrix Division is not possible (Matrix 2 is singular).\n")

} else {

cat("\nMatrix Division is only possible for square matrices.\n")

# Run the matrix operations

matrix_operations()
OUTPUT:

You might also like