From: Pontarelli, Brett <bpontar_at_amazon.com>

Date: Sat 18 Feb 2006 - 11:34:03 EST

R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide!

http://www.R-project.org/posting-guide.html

R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Feb 18 11:39:30 2006

Date: Sat 18 Feb 2006 - 11:34:03 EST

You're right I had N and M defined outside of the function and rook and rsub were picking up on that. The following is a bit better and more cleaned up version with i-th order option:

rook = function(Y,i) {

N = nrow(Y); M = ncol(Y); rsub = function(Z,i) { X = matrix(0,N,M); X[1:(N-i),] = X[1:(N-i),] + Z[(1+i):N,]; X[(1+i):N,] = X[(1+i):N,] + Z[1:(N-i),]; X[,1:(M-i)] = X[,1:(M-i)] + Z[,(1+i):M]; X[,(1+i):M] = X[,(1+i):M] + Z[,1:(M-i)]; return(X); } return(rsub(Y,i)/rsub(matrix(1,N,M),i));}

Notice that the variable "i" can be passed any value even one that causes an error.

--Brett

-----Original Message-----

From: Mills, Jason [mailto:Jason.Mills@afhe.ualberta.ca]
Sent: Friday, February 17, 2006 4:11 PM

To: Pontarelli, Brett

Subject: RE: [R] Matrix indexing in a loop

Hi Brett, thanks for the tip.

I tried this function on my sample matrix and got the error message "Error in rsub(fun) : object "N" not found". Your code looks like it should work, so I must be doing some wrong. I will continue to experiment.

As for the neighbor pattern, the convention follows the rules of chess. I would consider a 2nd order rook case to include only 4 elements:

a[i-2,j] a[i+2,j] a[i,j-2] a[i,j+2]

Even 3rd order Rook would still only include 4 elements. As another example, a Queen neighborhood includes 8 elements, independent of the lag order.

I haven't started using 3D arrays yet. I am attempting spatio-temporal analysis and I have thought about representing my landscape (two dimensions) over time (the third dimension). For now, I'm trying to get a handle on working in two dimensions.

Thanks.

Jason

-----Original Message-----

From: Pontarelli, Brett [mailto:bpontar@amazon.com]
Sent: Friday, February 17, 2006 4:04 PM

To: Mills, Jason; r-help@stat.math.ethz.ch
Subject: RE: [R] Matrix indexing in a loop

Do you have to use a loop? The following function should do what you want for the 1st order:

rook = function(Y) {

rsub = function(Z) { X = matrix(0,nrow(Z),ncol(Z)); X[1:(N-1),1:M] = X[1:(N-1),1:M] + Z[2:N,1:M]; X[2:N,1:M] = X[2:N,1:M] + Z[1:(N-1),1:M]; X[1:N,1:(M-1)] = X[1:N,1:(M-1)] + Z[1:N,2:M]; X[1:N,2:M] = X[1:N,2:M] + Z[1:N,1:(M-1)]; return(X); } return(rsub(Y)/rsub(matrix(1,nrow(Y),ncol(Y))));}

I'm not sure I understand how the higher orders work. For example, an interior element for the 1st order is always divided by 4. Is an interior element for a 3rd order divided by 4 or 8 or something else? Also, how are you implementing your 3D matrices?

--Brett

-----Original Message-----

From: r-help-bounces@stat.math.ethz.ch

[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Mills, Jason

Sent: Friday, February 17, 2006 1:36 PM

To: r-help@stat.math.ethz.ch

Subject: [R] Matrix indexing in a loop

How do you specify matrix location a[i,j] (or a[i-1,j], etc.) in a "for" loop?

[,1] [,2] [,3]

*[1,] 1 5 9
**[2,] 2 6 10
*

[3,] 3 7 11

[4,] 4 8 12

For each element a[i,j] in "fun", sum the 1st order (Rook's) neighbors:

[[alternative HTML version deleted]]

R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide!

http://www.R-project.org/posting-guide.html

R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Feb 18 11:39:30 2006

*
This archive was generated by hypermail 2.1.8
: Mon 20 Feb 2006 - 14:08:41 EST
*