[R] Filling matrix elements with a function

Aimee Kopolow alj27 at georgetown.edu
Tue Nov 6 15:03:17 CET 2012


Hi Jessica and Jim,

Thanks for the feedback,
Jessica: currently I am defining the functions as the same, but they
will be altered at some point soon. Also, I figured that defining the
matrix elements as functions would give a dual possibility to the
matrix entry depending on the conditions (i.e. output of WET(t))

Jim: I tried what you suggested and now have another error! The main
reason I was defining m1, m2 and m3 as a matrix with dimensions NP is
so that I can alter NP depending on how many patches I wish to model.
Naturally, typing out m1[1,2], m1[2,1] etc gets prohibitively
time-consuming for large matrices. When I use the original migration
code for initializing the summaries of incoming movement to each
patch, I get the error message: "Error in m1[i, j] * y[zz + k] :
non-numeric argument to binary operator"

The code is as follows:
s <- rep(0,NP*16)

  for (i in 1:NP)
  {
	z <- (i-1)*16

	for (k in 3:6)
	{
		for (j in 1:NP)
		{
			zz <- (j-1)*16
			s[z+k] <- s[z+k] + m1[i,j]*y[zz+k]
		}
	}
	
	for (k in 7:11)
	{
		for (j in 1:NP)
		{
			zz <- (j-1)*16
			s[z+k] <- s[z+k] + m2[i,j]*y[zz+k]
		}
	}

	for (k in 13:16)
	{
		for (j in 1:NP)
		{
			zz <- (j-1)*16
			s[z+k] <- s[z+k] + m3[i,j]*y[zz+k]
		}
	}


When I type in m1 to look at the matrix, it has NULL in the places I
need a 0, and a ? in the places I need output from WET(t), thus no
numeric output.

Any advice on what to do is greatly appreciated...


Aimee.



More information about the R-help mailing list