Estigarribia, Bruno
2013-May-27 15:24 UTC
[R] How sum all possible combinations of rows, given 4 matrices
Hello all, I have 4 matrices with 3 columns each (different number of rows though). I want to find a function that returns all possible 3-place vectors corresponding to the sum by columns of picking one row from matrix 1, one from matrix 2, one from matrix 3, and one from matrix 4. So basically, all possible ways of picking one row from each matrix and then sum their columns to obtain a 3-place vector. Is there a way to use expand.grid and reduce to obtain this result? Or am I on the wrong track? Thank you, Bruno PS:I believe I have given all relevant info. I apologize in advance if my question is ill-posed or ambiguous.
Bert Gunter
2013-May-27 16:30 UTC
[R] How sum all possible combinations of rows, given 4 matrices
Homework? We don't do homework here. -- Bert On Mon, May 27, 2013 at 8:24 AM, Estigarribia, Bruno <estigarr at email.unc.edu> wrote:> Hello all, > > I have 4 matrices with 3 columns each (different number of rows though). I > want to find a function that returns all possible 3-place vectors > corresponding to the sum by columns of picking one row from matrix 1, one > from matrix 2, one from matrix 3, and one from matrix 4. So basically, all > possible ways of picking one row from each matrix and then sum their > columns to obtain a 3-place vector. > Is there a way to use expand.grid and reduce to obtain this result? Or am > I on the wrong track? > Thank you, > Bruno > PS:I believe I have given all relevant info. I apologize in advance if my > question is ill-posed or ambiguous. > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Jeff Newmiller
2013-May-27 16:40 UTC
[R] How sum all possible combinations of rows, given 4 matrices
I expect the answer to involve manipulating indices. But why do you need to do this? This looks suspiciously like homework, and there is a no-homework policy on this list (see the Posting Guide). --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. "Estigarribia, Bruno" <estigarr at email.unc.edu> wrote:>Hello all, > >I have 4 matrices with 3 columns each (different number of rows >though). I >want to find a function that returns all possible 3-place vectors >corresponding to the sum by columns of picking one row from matrix 1, >one >from matrix 2, one from matrix 3, and one from matrix 4. So basically, >all >possible ways of picking one row from each matrix and then sum their >columns to obtain a 3-place vector. >Is there a way to use expand.grid and reduce to obtain this result? Or >am >I on the wrong track? >Thank you, >Bruno >PS:I believe I have given all relevant info. I apologize in advance if >my >question is ill-posed or ambiguous. > >______________________________________________ >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.
arun
2013-May-27 17:54 UTC
[R] How sum all possible combinations of rows, given 4 matrices
Hi, Not sure if this is what you expected: set.seed(24) mat1<- matrix(sample(1:20,3*4,replace=TRUE),ncol=3) set.seed(28) mat2<- matrix(sample(1:25,3*6,replace=TRUE),ncol=3) set.seed(30) mat3<- matrix(sample(1:35,3*8,replace=TRUE),ncol=3) set.seed(35) mat4<- matrix(sample(1:40,3*10,replace=TRUE),ncol=3) ?dat1<-expand.grid(seq(dim(mat1)[1]),seq(dim(mat2)[1]),seq(dim(mat3)[1]),seq(dim(mat4)[1])) vec1<-paste0("mat",1:4) matNew<-do.call(cbind,lapply(seq_len(ncol(dat1)),function(i) get(vec1[i])[dat1[,i],])) colnames(matNew)<- (seq(12)-1)%%3+1 datNew<-data.frame(matNew) res<-sapply(split(colnames(datNew),gsub("\\..*","",colnames(datNew))),function(x) rowSums(datNew[,x])) dim(res) #[1] 1920??? 3 ?head(res) #???? X1 X2 X3 #[1,] 46 63 70 #[2,] 45 68 59 #[3,] 55 55 66 #[4,] 51 65 61 #[5,] 48 84 75 #[6,] 47 89 64 A.K. ----- Original Message ----- From: "Estigarribia, Bruno" <estigarr at email.unc.edu> To: "r-help at R-project.org" <r-help at r-project.org> Cc: Sent: Monday, May 27, 2013 11:24 AM Subject: [R] How sum all possible combinations of rows, given 4 matrices Hello all, I have 4 matrices with 3 columns each (different number of rows though). I want to find a function that returns all possible 3-place vectors corresponding to the sum by columns of picking one row from matrix 1, one from matrix 2, one from matrix 3, and one from matrix 4. So basically, all possible ways of picking one row from each matrix and then sum their columns to obtain a 3-place vector. Is there a way to use expand.grid and reduce to obtain this result? Or am I on the wrong track? Thank you, Bruno PS:I believe I have given all relevant info. I apologize in advance if my question is ill-posed or ambiguous. ______________________________________________ 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.