I am currently trying to duplicate the diag() function in R to get a better understanding. Unfortunately, my solution is only useful for symmetric matrices.
diagnew <- function(x){
k <- c()
for(i in 1:nrow(x)){
k[i] <- x[i,i]
}
return(k)
}
I would be grateful for any tips that allow me to generalize my function.