I'm creating and storing a bunch of conditional matrices in vector form then I'm recalling them, returning their structure to matrix, and multiplying by a vector. This vector depends on the previous vector and the current matrix. I have tried to express this in the second loop as [,y+1] to index the vectors on the output matrix . While I get the desire result, I also get an error that aborts the program. I would appreciate suggestions on how to approach this.
env=rnorm(50, 22, 5)
les=matrix(nrow=9,ncol=length(env),byrow=T)
for (x in 1:length(env))
{
a=sqrt(env[x])-3
b=sqrt(env[x])-2
c=sqrt(env[x])-1
A=.9
B=.5
C=.2
les[,x]=c(a,b,c,A,0,0,0,B,0)
}
pop=matrix(nrow=3,ncol=length(env))
pop[,1]=c(1,2,2)
for (y in 1:length(env))
{
pop[,y+1]=pop[,y]%*%matrix(les[,y],3,3,T)
}
print(pop)
barplot(pop)