This is a practice question to prep for an exam. I'm given the following code:
W=matrix(1:16,byrow=T,ncol=4)
print(W)
fmat=function(W){
n=nrow(W)
for (i in 1:n){
for (j in 1:n){
W[j,i]=W[i,j]+W[j,i]
}
}
return(W)
}
print(fmat(W))
We have to "run" the code on paper and then check our answers by running the code in R. I wrote out the correct matrix for W but I got fmat(W) wrong. R gives me the following output for fmat(W):
[,1] [,2] [,3] [,4]
[1,] 2 9 15 21
[2,] 7 12 24 30
[3,] 12 17 22 39
[4,] 17 22 27 32
where I had written down that fmat(W) would equal:
[,1] [,2] [,3] [,4]
[1,] 2 7 12 17
[2,] 7 12 17 22
[3,] 12 17 22 27
[4,] 17 22 27 32
What exactly is going on here? I had interpreted the function to calculate that, for example, w[2,1]=w[1,2]+w[2,1], which is 2+5=7.