I'm trying to create a program that sums the diagonal of a matrix that I've created. I used the code below but I don't understand whats wrong with the code I made.
a <- c(1, 2, 3)
b <- c(3, 5, 6)
x <- matrix(a, b, nrow=3, ncol=3)
x
for (i in 1:nrow(x)) {
for (j in 1:ncol(x)) {
if (i=j) {
diags[i, j] <- sum(x[i, j])
}
}
}