Hello all, Consider the following problem: There are two vectors: rows <- c(1, 2, 3, 4, 5) columns <- c(10, 11, 12, 13, 14) I want to create a matrix with dimensions length(rows) x length(columns): res <- matrix(nrow = length(rows), ncol = length(columns)) If "i" and "j" are the row and column indexes respectively, the values of the cells are abs(rows[i] - columns[j]). The resultant matrix follows: [,1] [,2] [,3] [,4] [,5] [1,] 9 10 11 12 13 [2,] 8 9 10 11 12 [3,] 7 8 9 10 11 [4,] 6 7 8 9 10 [5,] 5 6 7 8 9 This matrix may be generated by using a simple "for" loop: for(i in 1:length(rows)){ for(j in 1:length(columns)){ res[i,j] <- abs(rows[i] - columns[j]) } } Is there a quicker, vector-based approach for doing this or a function included in the recommended packages that does this? Thanks! -- Dan Gerlanc Williams College
Hi, Try this: rows <- c(1, 2, 3, 4, 5) columns <- c(10, 11, 12, 13, 14) expanded <-expand.grid(rows,columns) abslist <-abs(ex$Var1-ex$Var2) res <- matrix(abslist,nrow = length(rows), ncol = length(columns)) Neurorox>From: "Daniel Gerlanc" <dgerlanc at gmail.com> >Reply-To: dgerlanc at gmail.com >To: r-help at stat.math.ethz.ch >Subject: [R] Vectorizing a "for" loop >Date: Thu, 3 Aug 2006 10:10:46 -0400 > >Hello all, > >Consider the following problem: > >There are two vectors: > >rows <- c(1, 2, 3, 4, 5) >columns <- c(10, 11, 12, 13, 14) > >I want to create a matrix with dimensions length(rows) x length(columns): > >res <- matrix(nrow = length(rows), ncol = length(columns)) > >If "i" and "j" are the row and column indexes respectively, the values >of the cells are abs(rows[i] - columns[j]). The resultant matrix >follows: > > [,1] [,2] [,3] [,4] [,5] >[1,] 9 10 11 12 13 >[2,] 8 9 10 11 12 >[3,] 7 8 9 10 11 >[4,] 6 7 8 9 10 >[5,] 5 6 7 8 9 > >This matrix may be generated by using a simple "for" loop: > >for(i in 1:length(rows)){ > for(j in 1:length(columns)){ > res[i,j] <- abs(rows[i] - columns[j]) > } >} > >Is there a quicker, vector-based approach for doing this or a function >included in the recommended packages that does this? > >Thanks! > >-- Dan Gerlanc >Williams College > >______________________________________________ >R-help at stat.math.ethz.ch 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.
On Thu, 3 Aug 2006, Daniel Gerlanc wrote:> Hello all, > > Consider the following problem: > > There are two vectors: > > rows <- c(1, 2, 3, 4, 5) > columns <- c(10, 11, 12, 13, 14) > > I want to create a matrix with dimensions length(rows) x length(columns): > > res <- matrix(nrow = length(rows), ncol = length(columns)) > > If "i" and "j" are the row and column indexes respectively, the values > of the cells are abs(rows[i] - columns[j]). The resultant matrix > follows: > > [,1] [,2] [,3] [,4] [,5] > [1,] 9 10 11 12 13 > [2,] 8 9 10 11 12 > [3,] 7 8 9 10 11 > [4,] 6 7 8 9 10 > [5,] 5 6 7 8 9 > > This matrix may be generated by using a simple "for" loop: > > for(i in 1:length(rows)){ > for(j in 1:length(columns)){ > res[i,j] <- abs(rows[i] - columns[j]) > } > } > > Is there a quicker, vector-based approach for doing this or a function > included in the recommended packages that does this? >abs(outer(rows,columns,"-")) -thomas
On Thu, 2006-08-03 at 10:10 -0400, Daniel Gerlanc wrote:> Hello all, > > Consider the following problem: > > There are two vectors: > > rows <- c(1, 2, 3, 4, 5) > columns <- c(10, 11, 12, 13, 14) > > I want to create a matrix with dimensions length(rows) x length(columns): > > res <- matrix(nrow = length(rows), ncol = length(columns)) > > If "i" and "j" are the row and column indexes respectively, the values > of the cells are abs(rows[i] - columns[j]). The resultant matrix > follows: > > [,1] [,2] [,3] [,4] [,5] > [1,] 9 10 11 12 13 > [2,] 8 9 10 11 12 > [3,] 7 8 9 10 11 > [4,] 6 7 8 9 10 > [5,] 5 6 7 8 9 > > This matrix may be generated by using a simple "for" loop: > > for(i in 1:length(rows)){ > for(j in 1:length(columns)){ > res[i,j] <- abs(rows[i] - columns[j]) > } > } > > Is there a quicker, vector-based approach for doing this or a function > included in the recommended packages that does this? > > Thanks!See ?outer> outer(rows, columns, function(x, y) abs(x - y))[,1] [,2] [,3] [,4] [,5] [1,] 9 10 11 12 13 [2,] 8 9 10 11 12 [3,] 7 8 9 10 11 [4,] 6 7 8 9 10 [5,] 5 6 7 8 9 HTH, Marc Schwartz
Hi outer(rows, columns, function(x,y) abs(x-y)) shall do it. HTH Petr On 3 Aug 2006 at 10:10, Daniel Gerlanc wrote: Date sent: Thu, 3 Aug 2006 10:10:46 -0400 From: "Daniel Gerlanc" <dgerlanc at gmail.com> To: r-help at stat.math.ethz.ch Subject: [R] Vectorizing a "for" loop Send reply to: dgerlanc at gmail.com <mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe> <mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>> Hello all, > > Consider the following problem: > > There are two vectors: > > rows <- c(1, 2, 3, 4, 5) > columns <- c(10, 11, 12, 13, 14) > > I want to create a matrix with dimensions length(rows) x > length(columns): > > res <- matrix(nrow = length(rows), ncol = length(columns)) > > If "i" and "j" are the row and column indexes respectively, the values > of the cells are abs(rows[i] - columns[j]). The resultant matrix > follows: > > [,1] [,2] [,3] [,4] [,5] > [1,] 9 10 11 12 13 > [2,] 8 9 10 11 12 > [3,] 7 8 9 10 11 > [4,] 6 7 8 9 10 > [5,] 5 6 7 8 9 > > This matrix may be generated by using a simple "for" loop: > > for(i in 1:length(rows)){ > for(j in 1:length(columns)){ > res[i,j] <- abs(rows[i] - columns[j]) > } > } > > Is there a quicker, vector-based approach for doing this or a function > included in the recommended packages that does this? > > Thanks! > > -- Dan Gerlanc > Williams College > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Petr Pikal petr.pikal at precheza.cz
Sorry, I used ex instead of expanded (was working name). rows <- c(1, 2, 3, 4, 5) columns <- c(10, 11, 12, 13, 14) expanded <-expand.grid(rows,columns) abslist <-abs(expanded$Var1-expanded$Var2) res <- matrix(abslist,nrow = length(rows), ncol = length(columns)) D?sol? encore Neuro>From: "Daniel Gerlanc" <dgerlanc at gmail.com> >Reply-To: dgerlanc at gmail.com >To: r-help at stat.math.ethz.ch >Subject: [R] Vectorizing a "for" loop >Date: Thu, 3 Aug 2006 10:10:46 -0400 > >Hello all, > >Consider the following problem: > >There are two vectors: > >rows <- c(1, 2, 3, 4, 5) >columns <- c(10, 11, 12, 13, 14) > >I want to create a matrix with dimensions length(rows) x length(columns): > >res <- matrix(nrow = length(rows), ncol = length(columns)) > >If "i" and "j" are the row and column indexes respectively, the values >of the cells are abs(rows[i] - columns[j]). The resultant matrix >follows: > > [,1] [,2] [,3] [,4] [,5] >[1,] 9 10 11 12 13 >[2,] 8 9 10 11 12 >[3,] 7 8 9 10 11 >[4,] 6 7 8 9 10 >[5,] 5 6 7 8 9 > >This matrix may be generated by using a simple "for" loop: > >for(i in 1:length(rows)){ > for(j in 1:length(columns)){ > res[i,j] <- abs(rows[i] - columns[j]) > } >} > >Is there a quicker, vector-based approach for doing this or a function >included in the recommended packages that does this? > >Thanks! > >-- Dan Gerlanc >Williams College > >______________________________________________ >R-help at stat.math.ethz.ch 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.
res<-outer(rows,columns,FUN=function(x,y) abs(x-y)) will help you. Am Thu, 03 Aug 2006 16:10:46 +0200 schrieb Daniel Gerlanc <dgerlanc at gmail.com>:> Hello all, > > Consider the following problem: > > There are two vectors: > > rows <- c(1, 2, 3, 4, 5) > columns <- c(10, 11, 12, 13, 14) > > I want to create a matrix with dimensions length(rows) x length(columns): > > res <- matrix(nrow = length(rows), ncol = length(columns)) > > If "i" and "j" are the row and column indexes respectively, the values > of the cells are abs(rows[i] - columns[j]). The resultant matrix > follows: > > [,1] [,2] [,3] [,4] [,5] > [1,] 9 10 11 12 13 > [2,] 8 9 10 11 12 > [3,] 7 8 9 10 11 > [4,] 6 7 8 9 10 > [5,] 5 6 7 8 9 > > This matrix may be generated by using a simple "for" loop: > > for(i in 1:length(rows)){ > for(j in 1:length(columns)){ > res[i,j] <- abs(rows[i] - columns[j]) > } > } > > Is there a quicker, vector-based approach for doing this or a function > included in the recommended packages that does this? > > Thanks! > > -- Dan Gerlanc > Williams College > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- -------------------- Universit?t Hamburg Institut f?r Statistik und ?konometrie Dipl.-Wi.-Math. Eik Vettorazzi Von-Melle-Park 5 20146 Hamburg Tel.: +49 40-42838-3540
try this: abs(outer(rows, columns, "-")) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Daniel Gerlanc" <dgerlanc at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Thursday, August 03, 2006 4:10 PM Subject: [R] Vectorizing a "for" loop> Hello all, > > Consider the following problem: > > There are two vectors: > > rows <- c(1, 2, 3, 4, 5) > columns <- c(10, 11, 12, 13, 14) > > I want to create a matrix with dimensions length(rows) x > length(columns): > > res <- matrix(nrow = length(rows), ncol = length(columns)) > > If "i" and "j" are the row and column indexes respectively, the > values > of the cells are abs(rows[i] - columns[j]). The resultant matrix > follows: > > [,1] [,2] [,3] [,4] [,5] > [1,] 9 10 11 12 13 > [2,] 8 9 10 11 12 > [3,] 7 8 9 10 11 > [4,] 6 7 8 9 10 > [5,] 5 6 7 8 9 > > This matrix may be generated by using a simple "for" loop: > > for(i in 1:length(rows)){ > for(j in 1:length(columns)){ > res[i,j] <- abs(rows[i] - columns[j]) > } > } > > Is there a quicker, vector-based approach for doing this or a > function > included in the recommended packages that does this? > > Thanks! > > -- Dan Gerlanc > Williams College > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm