[R] Filling matrix elements with a function

jim holtman jholtman at gmail.com
Tue Nov 6 17:16:13 CET 2012


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:
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list