0

I am trying to make my matrix (tc) symmetric (using R) by adding the corresponding entries and divide those by the sum of the corresponding diagonal entries (tc[i,j]+tc[j,i])/(tc[i,i]+tc[j,j]). I tried it with loops but it does not give me the right values let alone make the matrix symmetric. This is my code so far:

    for (i in 1:end){
      for(j in 1:end){
        tc[i,j]<-(tc[i,j]+tc[j,i])/(tc[i,i]+tc[j,j])
      }
    }

It's probably a super obvious mistake but I can't figure it out. Can anyone help me? =)

Linalein
  • 25
  • 5

1 Answers1

0

Well, if you think about it, you are summing using values that you have already updated (since you are looping over each i and j).

What if you make a new matrix with the same dimensions as tc, and then run your loop.

newTc <- matrix(0, nrow=nrow(tc), ncol=ncol(tc))
for (i in 1:end){
  for(j in 1:end){
    newTc[i,j]<-(tc[i,j]+tc[j,i])/(tc[i,i]+tc[j,j])
  }
}
devmacrile
  • 470
  • 2
  • 7
  • I actually did that this morning and your explanation makes so much sense. I did not think about it that way at all but of course you are completely right! Thank you! – Linalein Oct 02 '15 at 17:56