CMB123
2012-Oct-22 17:10 UTC
[R] Help with applying a function to all possible 2x2 submatrices
Hi all, I'm working with a large data set (on the order of 300X300) and trying to apply a function which compares the elements of all possible 2x2 submatrices. There are rc(r-1)(c-1) such submatrices, so obviously the naive method of looping through the rows and columns is computationally unfeasible for my data set: for(i in 1:(nrow(data)-1)) { for(j in (i+1):nrow(data)) { for (m in 1:(ncol(data)-1)) { for (n in (m+1):ncol(data)) { I have tried using the outer and apply functions to vectorize the task, but I can't seem to figure out the best method (or any method, for that matter) to help with what I'm trying to do. Any and all help is much appreciated! Thanks in advance, C -- View this message in context: http://r.789695.n4.nabble.com/Help-with-applying-a-function-to-all-possible-2x2-submatrices-tp4647048.html Sent from the R help mailing list archive at Nabble.com.
Sarah Goslee
2012-Oct-22 19:50 UTC
[R] Help with applying a function to all possible 2x2 submatrices
300x300 isn't terribly large; looping should work just fine. But I'm confused about a 2x2 submatrix: I would have thought that a submatrix would be adjacent elements, like x[1:2, 1:2] or x[13:14, 296:297] but your loop compares all possible sets of four elements, so the matrix position doesn't matter except for avoiding duplication. Maybe a bit more about what you're trying to accomplish (including the function you want to perform with those four elements) would be helpful. Sarah On Mon, Oct 22, 2012 at 1:10 PM, CMB123 <craig.bielski at gmail.com> wrote:> Hi all, > > I'm working with a large data set (on the order of 300X300) and trying to > apply a function which compares the elements of all possible 2x2 > submatrices. There are rc(r-1)(c-1) such submatrices, so obviously the naive > method of looping through the rows and columns is computationally unfeasible > for my data set: > > for(i in 1:(nrow(data)-1)) { > for(j in (i+1):nrow(data)) { > for (m in 1:(ncol(data)-1)) { > for (n in (m+1):ncol(data)) { > > I have tried using the outer and apply functions to vectorize the task, but > I can't seem to figure out the best method (or any method, for that matter) > to help with what I'm trying to do. > > Any and all help is much appreciated! Thanks in advance, > > C > >-- Sarah Goslee http://www.functionaldiversity.org
Rui Barradas
2012-Oct-22 20:07 UTC
[R] Help with applying a function to all possible 2x2 submatrices
Hello, If your matrix is in the order of 300x300, the problem of extracting all possible submatrices and applying a function will allways be a large one, but the use of ?combn may reduce it a bit if the order of rows/columns in the submatrices doesn't matter. It can reduce it from 300^4 = 8.1e+09 to 2.0e+09 submatrices, a factor of 4. See the example below. # Make up some data. nr <- nc <- 10 x <- matrix(rnorm(nr*nc), nrow = nr) cr <- combn(nr, 2) cc <- combn(nc, 2) fun <- function(rr, cc, xx, FUN) FUN(xx[rr, cc]) apply(cr, 2, function(i) apply(cc, 2, function(j) fun(cr[, i], cc[, j], x, sum) ) ) Note that this hides away the quartic nature of the algorithm. Hope this helps, Rui Barradas Em 22-10-2012 18:10, CMB123 escreveu:> Hi all, > > I'm working with a large data set (on the order of 300X300) and trying to > apply a function which compares the elements of all possible 2x2 > submatrices. There are rc(r-1)(c-1) such submatrices, so obviously the naive > method of looping through the rows and columns is computationally unfeasible > for my data set: > > for(i in 1:(nrow(data)-1)) { > for(j in (i+1):nrow(data)) { > for (m in 1:(ncol(data)-1)) { > for (n in (m+1):ncol(data)) { > > I have tried using the outer and apply functions to vectorize the task, but > I can't seem to figure out the best method (or any method, for that matter) > to help with what I'm trying to do. > > Any and all help is much appreciated! Thanks in advance, > > C > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-with-applying-a-function-to-all-possible-2x2-submatrices-tp4647048.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
CMB123
2012-Oct-22 20:10 UTC
[R] Help with applying a function to all possible 2x2 submatrices
I apologize if the term "submatrix" was confusing - I am basically 2 rows and 2 columns from a r x c matrix to construct 2x2 matricies. Thus, the choose(r,2) * choose(c,2) possible combinations. For each matrix [(a,b), (c,d)], I am testing a > b, b > d, d > c, and c > a. For the sake of simplicity I have combined these tests into a single function (with the intent of applying it to the matrix, ideally). I hope this is clearer... -- View this message in context: http://r.789695.n4.nabble.com/Help-with-applying-a-function-to-all-possible-2x2-submatrices-tp4647048p4647065.html Sent from the R help mailing list archive at Nabble.com.
Michael Friendly
2012-Oct-23 14:14 UTC
[R] Help with applying a function to all possible 2x2 submatrices
On 10/22/2012 1:10 PM, CMB123 wrote:> Hi all, > > I'm working with a large data set (on the order of 300X300) and trying to > apply a function which compares the elements of all possible 2x2 > submatrices. There are rc(r-1)(c-1) such submatrices, so obviously the naive > method of looping through the rows and columns is computationally unfeasible > for my data set: > > for(i in 1:(nrow(data)-1)) { > for(j in (i+1):nrow(data)) { > for (m in 1:(ncol(data)-1)) { > for (n in (m+1):ncol(data)) { > > I have tried using the outer and apply functions to vectorize the task, but > I can't seem to figure out the best method (or any method, for that matter) > to help with what I'm trying to do. >See the function vcdExtra::loddsratio for something similar -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA