[R] Accessing sub diagonals / spdiag in R ?
Hello, I'm attempting to access a specific number of sub diagonals in a MATRIX and have been accustomed to using spdiags in MATLAB or Octave. I've got a solution pieced together using for loops and it works though isn't vectorized and liable to run very slow for large matrices. As an example: A = 1 2 3 4 5 9 8 7 6 5 4 5 6 7 8 5 4 3 2 1 8 7 6 0 1 The subdiagonals are: 9,5,3,0 4,4,6 5,7 and 8, I know about lower.tri and can fetch the data in a resulting vector which ,in this case, would be: 9,4,5,8,5,4,7,3,6,0 though I would have to manipulate this some more to extract the other diagonals (imagine this being done for say a 1000 x 1000 matrix). I looked at CRAN and didn't see anything corresponding to spdiags. The closest package appeared to be the one relating to sparse matrices and band symmetry. Would you have any suggestions about 1) how to emulate spdiags or 2) working with the lower.tri returned-data and extracting the remaining diags efficiently. I can live with what I have but imagine that there is something more direct. Thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] R - Vectorization and Functional Programming Constructs
Hello, I am new to R (coming from Perl) and have what is, at least at this point, a philosophical question and a request for comment on some basic code. As I understand it - R emphasizes ,or at least supports, the functional programming model. I've come across some code that was markedly absent in for loops - and have been seeing some constructs that relate to functional programming and vectorized code (not that is at all unique to R of course). But I'm also new to the concept of vectorizing code. However, since I anticipate dealing with vectors of large sizes I think that this approach is probably going to serve well in terms of performance. As an example I anticipate having vector operations calling for shifting. I'll be shifting vectors to the right (or left) like below while maintaining the length and filling with zeros. Keep in mind I'll ultimately be dealing with vectors with very large length. >x <- c(0,3,2,1,0,0,0) >vlen <- length(x) [1] 7 One solution to accomplish the right shift is to do something like: >x=c(0,x[1:vlen-1]) >x 1] 0 0 3 2 1 0 0 this does the trick though I'm wondering if this is in the spirit of "Vectorization". I could make recursive function that would cycle through the whole vector eventually leaving it full of 0s thus ending the recursion. Though does this capture the spirit of R programming and vectorizing ? Are there more primitive operators "closer" to the underlying C code that would serve performance interests better ? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.