Re: [R] [fixed] vectorized nested loop: apply a function that takes two rows

2007-01-24 Thread Jose Quesada
Thanks Charles, Martin, Substantial improvement with the vectorized solution. Here is a quick benchmark: # The loop-based solution: nestedCos = function (x) { if (is(x, Matrix) ) { cos = array(NA, c(ncol(x), ncol(x))) for (i in 2:ncol(x)) {

Re: [R] [fixed] vectorized nested loop: apply a function that takes two rows

2007-01-24 Thread Peter Dalgaard
Jose Quesada wrote: Thanks Charles, Martin, Substantial improvement with the vectorized solution. Here is a quick benchmark: # The loop-based solution: nestedCos = function (x) { if (is(x, Matrix) ) { cos = array(NA, c(ncol(x), ncol(x))) for (i in

[R] [fixed] vectorized nested loop: apply a function that takes two rows

2007-01-23 Thread Jose Quesada
(Extremely sorry, disregard previous email as I hit send before pasting the latest version of the example; this one is smaller too) Dear R users, I want to apply a function that takes two vectors as input to all pairs (combinations (nrow(X), 2))of matrix rows in a matrix. I know that ideally,

Re: [R] [fixed] vectorized nested loop: apply a function that takes two rows

2007-01-23 Thread Charles C. Berry
I am rusty on 'Matrix', but I see there are crossprod methods for those classes. res - crossprod( x , x ) gives your result up to scale factors of sqrt(res[i,i]*res[j,j]), so something like diagnl - Diagonal( ncol(x), sqrt( diag( res ) ) final.res - diagnl %*% res

Re: [R] [fixed] vectorized nested loop: apply a function that takes two rows

2007-01-23 Thread Charles C. Berry
On Tue, 23 Jan 2007, Charles C. Berry wrote: I am rusty on 'Matrix', but I see there are crossprod methods for those classes. res - crossprod( x , x ) gives your result up to scale factors of sqrt(res[i,i]*res[j,j]), so something like diagnl - Diagonal( ncol(x), sqrt( diag(