Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
R program to find the adjoint of a matrix (#4823)
Browse files Browse the repository at this point in the history
* Written R program to print ascii value of a character

* r program to print pattern 1

* r program to swap two numbers without using third variable

* r program to convert string to pascalcase

* r program to find the adjoint of a matrix

---------

Co-authored-by: ApurvaR1 <ApurvaR1@gmail.com>
Co-authored-by: Harsh Raj <harshraj8843@gmail.com>
  • Loading branch information
3 people authored Dec 4, 2023
1 parent 7b57beb commit e6a5526
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Function to calculate the cofactor of an element in a matrix
cofactor <- function(mat, row, col) {
sub_mat <- mat[-row, -col] # Remove the row and column
return ((-1)^(row + col) * det(sub_mat))
}

# Function to calculate the cofactor matrix
cofactorMatrix <- function(mat) {
n <- nrow(mat)
cofactor_mat <- matrix(0, n, n)
for (i in 1:n) {
for (j in 1:n) {
cofactor_mat[i, j] <- cofactor(mat, i, j)
}
}
return (cofactor_mat)
}

# Function to find the adjoint of a matrix
findAdjoint <- function(mat) {
return (t(cofactorMatrix(mat))) # Transpose of the cofactor matrix
}

# Test the function with the input matrix
input_matrix <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = TRUE)
adjoint_matrix <- findAdjoint(input_matrix)
print(adjoint_matrix)

0 comments on commit e6a5526

Please sign in to comment.