powered by

row(x, as.factor = FALSE)

dim

x

ij

i

col

x <- matrix(1:12, 3, 4) # extract the diagonal of a matrix dx <- x[row(x) == col(x)] dx # create an identity 5-by-5 matrix x <- matrix(0, nrow = 5, ncol = 5) x[row(x) == col(x)] <- 1 x

Run the code above in your browser using DataLab