A1 <- array(1:20, dim=c(5, 4))
seq.mult <-
function(AO){
A<-AO
N <- dim(A)[1]
for(i in 1:(N-1)){
A[i+1, ] <- (matrix(AO[i,1:4],nrow=2,ncol=2,byrow=FALSE)
%*% matrix(AO[i+1,1:4],nrow=2,ncol=2,byrow=FALSE))
}
A
}
seq.mult(A1)
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 79 124 199 344
[3,] 102 157 242 397
[4,] 129 194 289 454
[5,] 160 235 340 515You got the error (or warning) by omitting the comma, writing A[t+1], where I wrote A[i+1,]. Any matrix is also a vector and can be addressed as such. If A is a 5 x 4 matrix, as in this example, then A[t+1] interprets it as a 1 x 20 vector.
I hope you won't object if I comment on a few other things I noticed: First, I assume your data reside in a 2541 x 4 matrix, not a 4 x 2541 matrix. The modified version of the function should work for the former but not the latter. Second, I don't know where "h0m" is defined; I assume you meant to write AO there.
Also, "t" is the function to transpose a matrix. While R is capable of distinguishing matrices from functions in most contexts, there are some contexts where it is not obvious what is requested, and it is therefore usually not prudent to use the name of a function for some other purpose. This is done routinely for function arguments. For example, the density function for Student's t distribution, dt, has an argument df for degrees of freedom, but df is also the density function for the F distribution. However, I try to avoid that, to the point of trying names I plan to use to see if they are already defined.
Your subject line is misleading: I saw no recursion in your question. A "recursion", according to the third (and seemingly most relevant here) definition in "www.m-w.com", is "a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself".
You may already know that the function to get eigenvalues and vectors is "eigen".
hope this helps. spencer graves
Ivan Kautter wrote:
List serv subscribers,
I am wondering if there is an efficient means of doing recursive matrix multiplication in R. My data resides in a 4 X 2541 matrix from which I need to extract 2541 2X2 matrices based on each row. If I attempt something like this:
function(AO)
{A<-AO
{for(t in 1:4)
A[t+1]<-matrix(h0m[t,1:4],nrow=2,ncol=2,byrow=FALSE)%*%matrix(h0m[t+1,1:4],nrow=2,ncol=2,byrow=FALSE)
} }
I get this type of error:
1: number of items to replace is not a multiple of replacement length 2: number of items to replace is not a multiple of replacement length 3: number of items to replace is not a multiple of replacement length 4: number of items to replace is not a multiple of replacement length
I believe I understand the error here in that I am attempting to add more elements to an object that is only a 2x2 matrix. So is there some way to just update the value of the matrix recursively? If you are asking yourself why I am bothering to do this if I mean to multiple only 4 matrices, I actually intend to multiple up to all 2541 of them, which seems no small task. Ultimately, I would like to define the time period over which I am multiplyig these essentially time-dependent matrices and obtain the eigenvalues of the final matrix product.
If anyone can offer any assistance, I would greatly appreciate it. Thanks.
Ivan Kautter
_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar – get it now! http://clk.atdmt.com/AVE/go/onm00200415ave/direct/01/
______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
