Hana Lee
2010-Nov-22 05:27 UTC
[R] Splitting 3D matrix from for loop to generate/save 2D matrices
Hi! I have a matrix called M with dimension (586,100,100). I would like to split and save this into 586 matrices with dimension 100 by 100. I have tried the following for loops but couldn't get it work.. l<-dim(M)[1] for (i in (1:l)){ save(M[i,,],file = "M_[i].img") } Can somebody help me with this? Thanks! Hana Lee [[alternative HTML version deleted]]
Henrik Bengtsson
2010-Nov-22 05:50 UTC
[R] Splitting 3D matrix from for loop to generate/save 2D matrices
Hi, On Sun, Nov 21, 2010 at 9:27 PM, Hana Lee <hanalee at email.unc.edu> wrote:> Hi! > > I have a matrix called M with dimension (586,100,100).First of all, In R it is only an object with *two* dimensions that is called "matrix". Anything with two or more dimensions is called an "array". Example:> x <- 1:(2*3*4) > y <- matrix(x, ncol=2) > z <- array(x, dim=c(2,3,4))# VECTORS> is.vector(x)[1] TRUE> is.vector(y)[1] FALSE> is.vector(z)[1] FALSE # MATRICES> is.matrix(x)[1] FALSE> is.matrix(y)[1] TRUE> is.matrix(z)[1] FALSE # ARRAYS> is.array(x)[1] FALSE> is.array(y)[1] TRUE> is.array(z)[1] TRUE So, you've got an *array* (not a matrix).> I would like to split > and save this into 586 matrices with dimension 100 by 100. > I have tried the following for loops but couldn't get it work.. > > l<-dim(M)[1] > for (i in (1:l)){ > save(M[i,,],file = "M_[i].img") > }"...but couldn't get it work [as I wanted]." The problem you have is generate a unique filename for matrix. The following two lines generate the same filename: filename <- sprintf("M_%d.img", i); filename <- paste("M_", i, ".img", sep=""); I prefer to use the sprintf() version. So, l <- dim(M)[1] for (i in (1:l)) { filename <- sprintf("M_%d.img", i); save(M[i,,], file=filename); } My $.02 /Henrik> > Can somebody help me with this? Thanks! > > Hana Lee > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
Michael Bedward
2010-Nov-22 05:55 UTC
[R] Splitting 3D matrix from for loop to generate/save 2D matrices
Hi Hana, Use the paste function to create your file names. for ( i in 1:dim(M)[1] ) save( M[i,,], file=paste("M_", i, ".img", sep="") ) Alternatively, use the sprintf function to get names with leading zeroes for easier sorting of files: for (i in 1:dim(M)[1] ) save( M[i,,], file=sprintf("M_%03d.img", i) ) Michael On 22 November 2010 16:27, Hana Lee <hanalee at email.unc.edu> wrote:> Hi! > > I have a matrix called M with dimension (586,100,100). I would like to split > and save this into 586 matrices with dimension 100 by 100. > I have tried the following for loops but couldn't get it work.. > > l<-dim(M)[1] > for (i in (1:l)){ > save(M[i,,],file = "M_[i].img") > } > > Can somebody help me with this? Thanks! > > Hana Lee > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
David Winsemius
2010-Nov-22 06:03 UTC
[R] Splitting 3D matrix from for loop to generate/save 2D matrices
On Nov 22, 2010, at 12:27 AM, Hana Lee wrote:> Hi! > > I have a matrix called M with dimension (586,100,100).In R you have an array (not a matrix) wehn the number of dimensions is 3.> I would like to split > and save this into 586 matrices with dimension 100 by 100.> I have tried the following for loops but couldn't get it work.. > > l<-dim(M)[1] > for (i in (1:l)){ > save(M[i,,],I think the save function needs a name rather than an object for evaluation. Also it's not a representation that will be particularly useful outside the context of R.> file = "M_[i].img")# the R interpreter is not going to evaluate those "i"'s inside quotes, no matter how smart you think it is.> } >Maybe (with some hesitation about the advisability of this): l<-dim(M)[1] for (i in (1:l)){ temp <- M[i,,] save(temp, file = paste("M_",i",".img", sep="") } When these get load()-ed back in, they will each have the have "temp", so if you read in more than one, only the last one will remain. It might make more sense to write them out as a group and read them back in the same way.> Can somebody help me with this? Thanks! > > Hana Lee > > [[alternative HTML version deleted]]-- David Winsemius, MD West Hartford, CT