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

2010-11-21 Thread David Winsemius


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

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


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

2010-11-21 Thread Henrik Bengtsson
Sorry, I did a mistake corrected below.

On Sun, Nov 21, 2010 at 9:50 PM, hb  wrote:
> Hi,
>
> On Sun, Nov 21, 2010 at 9:27 PM, Hana Lee  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]."

Indeed save(M[i,,], ...) will throw an error.  You also need to subset
to a temporary variable, e.g. X <- M[i,,], and save that.  See below.

>
> 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);
> }
>

l <- dim(M)[1]
for (i in (1:l)) {
  filename <- sprintf("M_%d.img", i);
  X <- M[i,,];
  save(X, file=filename);
}


Note that this will load the data into a variable called 'X' when you
load() one of the files.  If you do not want to have to worry about
the name you can use:

library("R.utils");
l <- dim(M)[1];
for (i in (1:l)) {
  filename <- sprintf("M_%d.img", i);
  saveObject(M[i,,], file=filename);
}

and later load the object into any variable you with:

filename <- "M_32.img";
Z <- loadObject(filename);

/H

> My $.02
>
> /Henrik
>
>
>
>>
>> Can somebody help me with this? Thanks!
>>
>> Hana Lee
>>
>>        [[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-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.


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

2010-11-21 Thread Michael Bedward
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  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@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-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.


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

2010-11-21 Thread Henrik Bengtsson
Hi,

On Sun, Nov 21, 2010 at 9:27 PM, Hana Lee  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@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-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] Splitting 3D matrix from for loop to generate/save 2D matrices

2010-11-21 Thread Hana Lee
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@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.