简体   繁体   English

如何生成变量名(i,j)列表,然后为它们赋值,尤其是双下标(使用R)

[英]How to produce a list of variable name(i,j), then assign values to them, especially for double subscript(using R)

I wonder if there is a simple way to produce a list of variables using a for loop, and give its value.我想知道是否有一种简单的方法可以使用 for 循环生成变量列表并给出其值。

Here I try to generate A11,A12,A13,A14,A21,A22,A23,A24,A31,A32,A33,A34,A41,A42,A43,A44, and assign a 6 by 6 matrix.这里我尝试生成 A11,A12,A13,A14,A21,A22,A23,A24,A31,A32,A33,A34,A41,A42,A43,A44,并分配一个 6 x 6 的矩阵。 After that I want to do some calculation.之后我想做一些计算。

The below is part of the code.下面是代码的一部分。 How to simplify it.(Using R)如何简化它。(使用 R)

for (i in 1:4){ assign(paste("A",i, sep=""), matrix(0,6,6)) }

for (i in 1:4){ assign(paste("A1",i, sep=""), matrix(0,6,6)) };A11;A12;A13;A14

for (i in 1:4){ assign(paste("A2",i, sep=""), matrix(0,6,6)) };A21;A22;A23;A24

for (i in 1:4){ assign(paste("A3",i, sep=""), matrix(0,6,6)) };A31;A32;A33;A34

for (i in 1:4){ assign(paste("A4",i, sep=""), matrix(0,6,6)) };A41;A42;A43;A44



for (i in 1:nrow(X)){
p<-YHatMulti(theta,X[i,])


A11<-(A11-as.numeric(p[,1]*(1-p[,1]))*(as.matrix(X[i,]))%*%t(as.matrix(X[i,])))

A22<-(A22-as.numeric(p[,2]*(1-p[,2]))*(as.matrix(X[i,]))%*%t(as.matrix(X[i,])))

A33<-(A33-as.numeric(p[,3]*(1-p[,3]))*(as.matrix(X[i,]))%*%t(as.matrix(X[i,])))

A44<-(A44-as.numeric(p[,4]*(1-p[,4]))*(as.matrix(X[i,]))%*%t(as.matrix(X[i,])))



A12<-A12+as.numeric(p[,1]*p[,2])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A13<-A13+as.numeric(p[,1]*p[,3])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))
A14<-A14+as.numeric(p[,1]*p[,4])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A21<-A21+as.numeric(p[,2]*p[,1])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A23<-A23+as.numeric(p[,2]*p[,3])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A24<-A24+as.numeric(p[,2]*p[,4])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A31<-A31+as.numeric(p[,3]*p[,1])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A32<-A32+as.numeric(p[,3]*p[,2])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A34<-A34+as.numeric(p[,3]*p[,4])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A41<-A41+as.numeric(p[,4]*p[,1])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A42<-A42+as.numeric(p[,4]*p[,2])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))

A43<-A43+as.numeric(p[,4]*p[,3])*(as.matrix(X[i,]))%*%t(as.matrix(X[i,]))




A1<-rbind(A11,A12,A13,A14)

A2<-rbind(A21,A22,A23,A24)

A3<-rbind(A31,A32,A33,A34)

A4<-rbind(A41,A42,A43,A44)

A<-cbind(A1,A2,A3,A4)

}

A

One way to simplify this code is to stop using variables called A1 , A2 , etc.简化此代码的一种方法是停止使用名为A1A2等的变量。

Instead, you can create list of lists:相反,您可以创建列表列表:

A <- list()

# Initalize sub-lists
for (i in 1:4) {
    A[[i]] <- list()
}

Or, somewhat more compactly,或者,更紧凑一点,

A <- replicate(4, list())

Then instead of assigning to A11 you can assign to A[[1]][[1]] :然后,您可以分配给A[[1]][[1]]而不是分配给A11

A[[1]][[1]] <- matrix(0,6,6))

But you can do better!但你可以做得更好! Check out what happens here:看看这里发生了什么:

A <- matrix(list(), 4, 4)

This is actually a matrix , but it's also a list, and can contain other matrices:这实际上是一个matrix ,但它也是一个列表,并且可以包含其他矩阵:

A[[1,1]] <- matrix(0,6,6))

So we can simplify this code所以我们可以简化这段代码

for (i in 1:nrow(X)) {
  A11<-(A11-as.numeric(p[,1]*(1-p[,1]))*(as.matrix(X[i,]))%*%t(as.matrix(X[i,])))
  A22<-(A22-as.numeric(p[,2]*(1-p[,2]))*(as.matrix(X[i,]))%*%t(as.matrix(X[i,])))
  A33<-(A33-as.numeric(p[,3]*(1-p[,3]))*(as.matrix(X[i,]))%*%t(as.matrix(X[i,])))
  A44<-(A44-as.numeric(p[,4]*(1-p[,4]))*(as.matrix(X[i,]))%*%t(as.matrix(X[i,])))
}

to this对此

for (i in 1:nrow(X)) {
  xi <- as.matrix(X[i,])
  xi_norm <- xi %*% t(xi)
  for (k in 1:4) {
    A[[k, k]] <-(A[[k, k]] - as.numeric(p[,k]*(1-p[,k])) * xi_norm
  }
}

At the end, we can construct the final matrix A as follows:最后,我们可以构造最终矩阵A如下:

bind_A_row <- function(row_index) {
  do.call(rbind, A[row_index, ])
}

A_rows <- lapply(1:4, bind_A_row)
A <- do.call(cbind, A_rows)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM