Dear list Having read through all the archives of the mailing lists, I've had no luck finding the answer to my problem. I am running a for(i in 1:n) loop and am creating a matrix, p, for each of the n observations. This matrix p is a 2x1 matrix. I am then doing the following: diagp<-diag(c(p[1,1],p[2,1])) to create a matrix with the elements of P on the diagonal (i.e. a 4x4 matrix). This works fine when I have n=60, however when I increase the size of the data set to n=100, I get the error message: Error in diag(c(p[1, 1], p[2, 1])) : subscript out of bounds I put this down to a problem with memory at first so increased the memory in my PC from 128Mb to 256Mb yet it made no difference and the error still occurred. I am eventually going to need to run this command for n=8000. Could anybody suggest what I can do to solve this problem? Do I need to store my matrix as another type of variable? Thanks in advance for any help Laura -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Can you give us just a little bit more detail, or an example (with simulated data if necessary) that recreates your problem? I'm a little bit confused: if p is a 2x1 matrix (dim(p)==c(2,1)), then your diag() command below should give a 2x2 matrix. Also, from just the detail you've given it's hard to see how n could make any difference at all. Is it possible that for some value of n you're getting an empty matrix which is triggering the error? Ben Bolker On Mon, 12 Nov 2001, Laura Gross wrote:> Dear list > > Having read through all the archives of the mailing lists, I've had no > luck finding the answer to my problem. > > I am running a for(i in 1:n) loop and am creating a matrix, p, for each > of the n observations. This matrix p is a 2x1 matrix. I am then doing > the following: > > diagp<-diag(c(p[1,1],p[2,1])) > > to create a matrix with the elements of P on the diagonal (i.e. a 4x4 > matrix). > > This works fine when I have n=60, however when I increase the size of > the data set to n=100, I get the error message: > > Error in diag(c(p[1, 1], p[2, 1])) : subscript out of bounds > > I put this down to a problem with memory at first so increased the > memory in my PC from 128Mb to 256Mb yet it made no difference and the > error still occurred. I am eventually going to need to run this command > for n=8000. > > Could anybody suggest what I can do to solve this problem? Do I need to > store my matrix as another type of variable? > > Thanks in advance for any help > Laura > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, the "subscript out of bounds" error nas nothing to do with memory. It reads exactly what it says: that subscript (index) is out of given bounds of a vector/matrix/general array... Do you still have p as a _matrix_ 2by1?, not as a _vector_ of length 2? Moreover, if p is 2by1 matrix, diagp<-diag(c(p[1,1],p[2,1])) is 2by2, not 4by4. Anyway, store p using as.matrix command (or try ?as.matrix for help) Jan ------------------------------------------------- designed for _monospaced_ font ------------------------------------------------- /- Jan Svatos, PhD Sokolovska 855/225 -/ /- Data Analyst, Prague 9 -/ /- Eurotel Praha 190 00 -/ /- jan_svatos at eurotel.cz Czechia -/ ------------------------------------------------- - - - Original message: - - - From: owner-r-help at stat.math.ethz.ch Send: 11/12/01 1:03:53 PM To: r-help at stat.math.ethz.ch Subject: [R] 'subscript out of bounds' in matrices Dear list Having read through all the archives of the mailing lists, I've had no luck finding the answer to my problem. I am running a for(i in 1:n) loop and am creating a matrix, p, for each of the n observations. This matrix p is a 2x1 matrix. I am then doing the following: diagp<-diag(c(p[1,1],p[2,1])) to create a matrix with the elements of P on the diagonal (i.e. a 4x4 matrix). This works fine when I have n=60, however when I increase the size of the data set to n=100, I get the error message: Error in diag(c(p[1, 1], p[2, 1])) : subscript out of bounds I put this down to a problem with memory at first so increased the memory in my PC from 128Mb to 256Mb yet it made no difference and the error still occurred. I am eventually going to need to run this command for n=8000. Could anybody suggest what I can do to solve this problem? Do I need to store my matrix as another type of variable? Thanks in advance for any help Laura -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I'm not entirely sure what's wrong with the code you have, but you might be better off doing the following: If p is a 2x1 matrix and you want to make a diagonal matrix with elements of p on the diagonal, maybe: diagp <- diag(as.vector(p)) would work better? -roger _____________________________ UCLA Department of Statistics rpeng at stat.ucla.edu On Mon, 12 Nov 2001, Laura Gross wrote:> Dear list > > Having read through all the archives of the mailing lists, I've had no > luck finding the answer to my problem. > > I am running a for(i in 1:n) loop and am creating a matrix, p, for each > of the n observations. This matrix p is a 2x1 matrix. I am then doing > the following: > > diagp<-diag(c(p[1,1],p[2,1])) > > to create a matrix with the elements of P on the diagonal (i.e. a 4x4 > matrix). > > This works fine when I have n=60, however when I increase the size of > the data set to n=100, I get the error message: > > Error in diag(c(p[1, 1], p[2, 1])) : subscript out of bounds > > I put this down to a problem with memory at first so increased the > memory in my PC from 128Mb to 256Mb yet it made no difference and the > error still occurred. I am eventually going to need to run this command > for n=8000. > > Could anybody suggest what I can do to solve this problem? Do I need to > store my matrix as another type of variable? > > Thanks in advance for any help > Laura > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._