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

3 3 Matrices

The document defines functions for inputting, printing, transposing, adding, subtracting, and multiplying 3x3 matrices. It then demonstrates calling the functions to: input matrices A and B from the user, take the transpose of A and B, add and subtract A and B, and multiply A and B together. The results of each matrix operation are then printed.

Uploaded by

roshan.csds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

3 3 Matrices

The document defines functions for inputting, printing, transposing, adding, subtracting, and multiplying 3x3 matrices. It then demonstrates calling the functions to: input matrices A and B from the user, take the transpose of A and B, add and subtract A and B, and multiply A and B together. The results of each matrix operation are then printed.

Uploaded by

roshan.csds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

3*3 Matrices

#Function to input a matrix from the user.

input_matrix<-function(rows,cols){
cat("Enter matrix elements row-wise:\n")
matrix_data<-numeric(rows*cols)

for(i in 1:(rows*cols)){
matrix_data[i]<-as.numeric(readline(prompt=""))
}
return(matrix(matrix_data,nrow=rows,ncol=cols,byrow=TRUE))
}

#Function to print a matrix


print_matrix<-function(matrix){
cat("Matrix:\n")
print(matrix)
}

#Function to calculate the transpose of a matrix


transpose_matrix<-function(matrix){
return(t(matrix))
}

#Function to perform matrix addition.


add_matrices<-function(matrix1,matrix2){
return(matrix1+matrix2)
}

#Function to perform matrix subtraction.


subtract_matrices<-function(matrix1,matrix2){
return(matrix1-matrix2)
}

#Function to perform matrix multiplication.


multiply_matrices<-function(matrix1,matrix2){
return(matrix1%*%matrix2)
}

#Input matrixes A and B


A<-input_matrix(3,3)
B<-input_matrix(3,3)

#Display matrices
print_matrix(A)
print_matrix(B)

#a]Transpose of the matrices


transposed_A<-transpose_matrix(A)
transposed_B<-transpose_matrix(B)

cat("\na)Transpose of matrix A:\n")


print_matrix(transposed_A)

cat("\na)Transpose of matrix B:\n")


print_matrix(transposed_B)

#b) Addition of the matrices


sum_matrix<-add_matrices(A,B)
cat("\nb)Addition of matrices A and B:\n")
print_matrix(sum_matrix)

#c) subtraction of the matrices


difference_matrix<-subtract_matrices(A,B)
cat("\nc)Subtraction of matrices A and B:\n")
print_matrix(difference_matrix)
#d) Multiplication of the matrices
product_matrix<-multiply_matrices(A,B)
cat("\nd)Multiplication of matrices A and B:\n")
print_matrix(product_matrix)

Output:
Enter matrix elements row-wise:
3
4
5
6
7
8
9
10
11
Enter matrix elements row-wise:
2
1
3
5
6
7
8
9
10
Matrix:
[,1] [,2] [,3]
[1,] 3 4 5
[2,] 6 7 8
[3,] 9 10 11
Matrix:
[,1] [,2] [,3]
[1,] 2 1 3
[2,] 5 6 7
[3,] 8 9 10

a)Transpose of matrix A:
Matrix:
[,1] [,2] [,3]
[1,] 3 6 9
[2,] 4 7 10
[3,] 5 8 11

a)Transpose of matrix B:
Matrix:
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 1 6 9
[3,] 3 7 10

b)Addition of matrices A and B:


Matrix:
[,1] [,2] [,3]
[1,] 5 5 8
[2,] 11 13 15
[3,] 17 19 21

c)Subtraction of matrices A and B:


Matrix:
[,1] [,2] [,3]
[1,] 1 3 2
[2,] 1 1 1
[3,] 1 1 1

d)Multiplication of matrices A and B:


Matrix:
[,1] [,2] [,3]
[1,] 66 72 87
[2,] 111 120 147
[3,] 156 168 207

You might also like