Folkes, Michael
2011-Mar-08 09:14 UTC
[R] repeat matrix column within each array third dimension
Hello all, I'm working with a matrix that will have varying dimensions. It will populate an array such that the number of matrix columns will determine the number of 3rd dimension levels of the array. Rows will be the same for both. For this example lets say the array will have 2 columns, but that's not fixed. dim(arr)<-c(dim(mat)[1],2,dim(mat)[2]) I wish to repeat each matrix column in the 3rd dimension of an array as follows in "desired appearance" below, but haven't quite got my generic array() function right. I need the equivalent of byrow=TRUE when filling in the array. thanks for all your help! mat<-matrix(1:12,ncol=3) mat desired.arr<-array(NA,dim=c(dim(mat)[1],2,dim(mat)[2])) #build array using generic code like this? # need something like byrow=TRUE array(rep(mat[1:dim(mat)[1],],each=dim(desired.arr)[2]),dim=dim(desired.arr)) # array needs to visually look as follows: "desired appearance: " array(c(rep(1:4,2),rep(5:8,2),rep(9:12,2)),dim=dim(desired.arr)) Michael Folkes [[alternative HTML version deleted]]
Uwe Ligges
2011-Mar-09 19:14 UTC
[R] repeat matrix column within each array third dimension
See ?aperm for changing the dimensions afterwards. Uwe Ligges On 08.03.2011 10:14, Folkes, Michael wrote:> Hello all, > I'm working with a matrix that will have varying dimensions. It will populate an array such that the number of matrix columns will determine the number of 3rd dimension levels of the array. Rows will be the same for both. For this example lets say the array will have 2 columns, but that's not fixed. > > dim(arr)<-c(dim(mat)[1],2,dim(mat)[2]) > > I wish to repeat each matrix column in the 3rd dimension of an array as follows in "desired appearance" below, but haven't quite got my generic array() function right. I need the equivalent of byrow=TRUE when filling in the array. > thanks for all your help! > > > mat<-matrix(1:12,ncol=3) > mat > desired.arr<-array(NA,dim=c(dim(mat)[1],2,dim(mat)[2])) > > #build array using generic code like this? > # need something like byrow=TRUE > array(rep(mat[1:dim(mat)[1],],each=dim(desired.arr)[2]),dim=dim(desired.arr)) > > # array needs to visually look as follows: > "desired appearance:" > array(c(rep(1:4,2),rep(5:8,2),rep(9:12,2)),dim=dim(desired.arr)) > > > Michael Folkes > > [[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.
Reasonably Related Threads
- Apply approx() to an array and eventually a list of arrays
- how to cut a multidimensional array along a chosen dimension and store each piece into a list
- Assign Formulas to Arrays or Matrices?
- Amount of memory occupied by data type
- [BioC] function to find coodinates in an array