hi..it\'s R-code promlem. Below R-code is det function. and this code can be use
ID: 3246525 • Letter: H
Question
hi..it's R-code promlem.
Below R-code is det function.
and this code can be used only 3x3 matrix.
i want to expand this code can possible in nxn matrix
so How can make this ??
########
mydet <- function(x) {
det2 <- function(a) {
a[1,1]*a[2,2] - a[1,2]*a[2,1]
}
abc <- 0
n <- nrow(x)
for ( j in 1:n) {
if ( j %% 2 == 0 ) {
y <- -1
} else {
y <- 1
}
abc <- abc + y * x[1,j]*det2(x[-1,-j])
}
abc
}
Explanation / Answer
######
mydet=function(x)
{
det2 <- function(a) {
a[1,1]*a[2,2] - a[1,2]*a[2,1]
}
abc=0
n=nrow(x)
if(n>2)
{
for ( j in 1:n)
{
if ( j %% 2 == 0 )
{
y <- -1
}
else
{
y <- 1
}
abc <- abc + y * x[1,j]*mydet((x[-1,-j]))
}
}
if(n==2)
{
abc=det2(x)
}
if(n==1)
abc=x[1,1]
return(abc)
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.