[R] Splitting 3D matrix from for loop to generate/save 2D matrices

Michael Bedward michael.bedward at gmail.com
Mon Nov 22 06:55:14 CET 2010


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.
>



More information about the R-help mailing list