Skip to content

Filling matrix elements with a function

6 messages · Aimee Kopolow, Jessica Streicher, arun +1 more

#
Hi all,


I have a matrix simulating migration in a spatial model. I want to be
able to define movement (the values of m1, m2 and m3) as only != 0
between adjacent patches and contingent on certain conditions as
defined in the function.

Here is the code:

WET<-function(t) {everglades$precipitation[t]}    #simply reads
precipitation data from a csv, value is numeric

AB<-function(WET,t) {ifelse(WET(t)>0, 0.0001, 0)}
BC<-function(WET,t) {ifelse(WET(t)>0, 0.0001, 0)}

NP<-4
 m1 <- matrix(0, NP,NP)
  m2 <- matrix(0,NP,NP)
  m3 <- matrix(0,NP,NP)


for (i in 1:NP)
  {
   for (j in 1:NP)
   {
    if ((i%%sqrt(NP)) == 0 & i!=j)
    {
     if(j == i+sqrt(NP) | j == i-1 | j == i-sqrt(NP))
     {
      m1[j,i] <- AB
      m1[i,j] <- AB


      m2[j,i] <- 0.00025
      m2[i,j] <- 0.00025

      m3[j,i] <- BC

      m3[i,j] <- BC

     }
    }
    if (i%%sqrt(NP) == 1)
    {
     if(j == i+sqrt(NP) | j == i+1 | j == i-sqrt(NP) )
     {
      m1[j,i] <- AB

      m1[i,j] <- AB


      m2[j,i] <- 0.00025
      m2[i,j] <- 0.00025

      m3[j,i] <- BC

      m3[i,j] <- BC
     }
    }
				
    if (i%%sqrt(NP)!=0 & i%%sqrt(NP)!=1 & i!=j)
    {
     if(j == i+sqrt(NP) | j == i+1 | j == i-sqrt(NP) | j == i-1)
     {
      m1[j,i] <- AB

      m1[i,j] <- AB

      m2[j,i] <- 0.00025
      m2[i,j] <- 0.00025

      m3[j,i] <- BC

      m3[i,j] <- BC
      }
    }
   }
  }

Please could somebody advise me as to where I'm going wrong and how to
get it right? I have tried several different permutations and can't
seem to do so. I tried googling keywords "inserting function as matrix
element" but couldn't find any advice online. I'm very inexperienced
at coding so apologies if this is very simple.

Thank you for any assistance you are able to give,
Aimee.
#
Not that i really understand what that shall do, but..

why do you define 2 functions that do exactly the same?
why do you want to make a matrix of functions instead of a matrix with the results of functions?
On 06.11.2012, at 05:11, Aimee Kopolow wrote:

            
#
Try this if you want a matrix of functions:
[1] 1764
[1] 9
On Tue, Nov 6, 2012 at 6:01 AM, Jessica Streicher
<j.streicher at micromata.de> wrote:

  
    
#
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.
#
Hi,
I guess this should also work.
f.1 <- function(x) x * x
list1<-lapply(rep(list(f.1),25),function(x) x)
mat1<-array(unlist(list1),dim=c(5,5))
?mat1[[1,1]](3)
#[1] 9

A.K.




----- Original Message -----
From: jim holtman <jholtman at gmail.com>
To: Jessica Streicher <j.streicher at micromata.de>
Cc: r-help <r-help at r-project.org>; Aimee Kopolow <alj27 at georgetown.edu>
Sent: Tuesday, November 6, 2012 8:10 AM
Subject: Re: [R] Filling matrix elements with a function

Try this if you want a matrix of functions:
[1] 1764
[1] 9
On Tue, Nov 6, 2012 at 6:01 AM, Jessica Streicher
<j.streicher at micromata.de> wrote:

  
    
#
You need to learn how to debug your code.  The most important thing to
do is to add:

options(error=utils::recover)

This will drop you into the 'browser' at the point of the error and
then you can examine each of the objects to see where the problem is.
As the error says, you have a non-numeric argument, so find out where
it is.  Do an 'str' of all the objects at the point of the error.

We can not debug it for you since we have none of the objects you are
using at the point of the error.  That is the reason for being able to
post a reproducible script.
On Tue, Nov 6, 2012 at 9:03 AM, Aimee Kopolow <alj27 at georgetown.edu> wrote: