Suppose I have a 4-D array X with dimensions (dx, dy, dz, dp). I want to collapse the first 3 dimensions of X to make a 2-D array Y with dimensions (dx*dy*dz, dp). Instead of awkward looping, what is a good way to do this? Is there a similar function like reshape in Matlab? Thanks, Gang
dim(X) <- c(prod(dim(X)[1:3]), dim(X)[4]) ? On Feb 28, 2008, at 3:05 PM, Gang Chen wrote:> Suppose I have a 4-D array X with dimensions (dx, dy, dz, dp). I want > to collapse the first 3 dimensions of X to make a 2-D array Y with > dimensions (dx*dy*dz, dp). Instead of awkward looping, what is a good > way to do this? Is there a similar function like reshape in Matlab? > > Thanks, > Gang > > ______________________________________________ > 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.
Try this also: apply(x, 4, rbind) On 28/02/2008, Gang Chen <gangchen at mail.nih.gov> wrote:> Suppose I have a 4-D array X with dimensions (dx, dy, dz, dp). I want > to collapse the first 3 dimensions of X to make a 2-D array Y with > dimensions (dx*dy*dz, dp). Instead of awkward looping, what is a good > way to do this? Is there a similar function like reshape in Matlab? > > Thanks, > Gang > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Thanks a lot for all the suggestions! Gang On Feb 28, 2008, at 3:20 PM, Henrique Dallazuanna wrote:> Try this also: > > apply(x, 4, rbind) > > On 28/02/2008, Gang Chen <gangchen at mail.nih.gov> wrote: >> Suppose I have a 4-D array X with dimensions (dx, dy, dz, dp). I want >> to collapse the first 3 dimensions of X to make a 2-D array Y with >> dimensions (dx*dy*dz, dp). Instead of awkward looping, what is a >> good >> way to do this? Is there a similar function like reshape in Matlab? >> >> Thanks, >> Gang