In a matrix Z of elements (i, j) how to assign the value 1 when i = j? (software R)

3

By the following condition:

ifelse(??, 1, Z/(1+1))

What to put in place of "??" so that R understands that when i = j in the matrix Z, I want to assign the value 1?

    
asked by anonymous 17.06.2017 / 03:38

1 answer

5

I do not see a simple way of solving your question. But I propose two forms of resolution:

Assign the desired value to the diagonal, directly.

a <- matrix(rep(0,100), nrow = 10)
diag(a) <- 1

Analyze the position to assign the value.

a <- matrix(rep(0,100), nrow = 10)
for(i in 1:dim(a)[1]){
  for(j in 1:dim(a)[2]){
    if(i == j){
      a[i,j] <- 1
    }
  }
}
    
17.06.2017 / 21:08