dim <- c(5, 4)
X <- matrix(NA_integer_, nrow = dim[1], ncol = dim[2])
Y <- t(X)
idxs <- seq_along(X)
# Assign by columns
X[idxs] <- idxs
print(X)
# Assign by rows
Y[indexByRow(dim(Y), idxs)] <- idxs
print(Y)
stopifnot(X == t(Y))
Run the code above in your browser using DataLab