Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用矩阵的条目更改矩阵中的条目_R_Loops_Matrix_Symmetric - Fatal编程技术网

R 使用矩阵的条目更改矩阵中的条目

R 使用矩阵的条目更改矩阵中的条目,r,loops,matrix,symmetric,R,Loops,Matrix,Symmetric,我试图使我的矩阵(tc)对称(使用R),方法是将相应的条目相加,然后除以相应的对角线条目之和(tc[I,j]+tc[j,I])/(tc[I,I]+tc[j,j])。我尝试了循环,但它没有给我正确的值,更不用说使矩阵对称了。这是我目前的代码: 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]) } } for(1中的i:end)

我试图使我的矩阵(tc)对称(使用R),方法是将相应的条目相加,然后除以相应的对角线条目之和(tc[I,j]+tc[j,I])/(tc[I,I]+tc[j,j])。我尝试了循环,但它没有给我正确的值,更不用说使矩阵对称了。这是我目前的代码:

    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])
      }
    }
for(1中的i:end){
对于(j在1中:结束){

tc[i,j]好吧,如果你仔细想想,你是在用你已经更新过的值求和(因为你在每个i和j上循环)

如果您创建一个与tc具有相同维度的新矩阵,然后运行循环会怎么样

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])
  }
}

newTc请提供一个可复制的例子什么是tc?你能在上面使用命令dput吗?我今天早上确实这么做了,你的解释很有道理。我根本没有这样想,但你当然完全正确!谢谢!