I wonder if there is a simple way to produce a list of variables using a for loop, and give its value.
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. After that I want to do some calculation.
The below is part of the code. How to simplify it.(Using 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