I want to implement a command in R which generates a nxn matrix A, such that each element aij = 2^|j-k|. I have the below code but I was wondering if there was something more elegant and precise rather than a nested loop?
A = matrix(1, nrow = n, ncol = n)
for (j in 1:n) {
for(k in 1:n) {
A[j,k] <- 2^(abs(j-k))}
}