[R] Matrix looping
Guenther, Cameron
Cameron.Guenther at MyFWC.com
Fri Mar 2 17:38:59 CET 2007
Hi all,
I am having a problem getting my fucntion to work correctly.
Here is my problem.
I have three ages: Nage<-c(1,2,3)
I have an weight matrix: Wt<-c( 0.04952867, 0.23808432, 0.34263880)
I have an age schedule of maturity: Mat<-c(0,1,1) where 0 is not mature,
and 1 is mature
I have a vulnerability schedule: Vul<-c(0,1,1)
I have an survivorship schedule: Survship<-c(1,0.4,0.16)
I also have leading parameters R0<-130.66; recK<-3.068; a<-5.48;
b<-0.0282; S<-0.4
I have annual catches for 100 years, ct<-runif(100,5,20)
Now I want a matrix of 100 years x 3 ages
yr<-c(1:100)
Nt<-matrix(0,nrow=length(yr)+1),ncol=length(Nage))
Now the first row of my matrix needs to be the product of R0 and
Survship, no problem Nt[1,]<-Ro*Survship
I also need to create a new vector of egg production so
Eggs<-vector();Eggs[1]<-sum(Mat*Nt[1,])
I also calculate the vulnerable biomass for each year so
VulBio<-vector();VulBio[1]<-sum(Wt*Vul*Nt[1,])
Now I calculate the exploitation Ut<-min(0.99,ct[1]/VulBio[1])
For (i in 1:length(Nt[,1])){
Now I need to calculate the first column of values where;
Nt[i+1,1]<-a*Eggs[i]/(1+b*Eggs[i])
Eggs[i+1]<-sum(Mat*Nt[i+1,]
VulBio[i+1]<-sum(Wt*Vul*Nt[i+1,])
Ut[i+1]<-min(0.99,ct[i+1]/VulBio[i+1])
Now here is the rub, I need to calculate the rest of the matrix based on
the previous years values for the previous age, but I don't want to
overwrite the first column or first row.
Nt[i+1,i+1]<-Nt[i,i]*S*(1-Ut[i]*Vul[i] gives the diagonal. How do I
fill the matrix without overwriting colum and row 1?
Cameron Guenther, Ph.D.
100 8th Ave. SE
St. Petersburg, Fl 33701
727-896-8626 ext. 4305
cameron.guenther at myfwc.com
More information about the R-help
mailing list