Re: [R] Applying a function to each element of an array

2005-10-07 Thread Kjetil Holuerson
Tim Smith wrote: > Hi, > > I have a 7000x7000 matrix, and each element is an integer. For each element, > I want to apply the function : > > wt <- 0 > for(q in 1:count){ > wt <- wt + 0.5^(q-1) > } > This is not a function! Maybe you want helper <- function(count) sum(0.5^((1:count)-1))

Re: [R] Applying a function to each element of an array

2005-10-07 Thread Ravi Varadhan
tober 07, 2005 4:29 PM > To: Tim Smith > Cc: r-help@stat.math.ethz.ch > Subject: Re: [R] Applying a function to each element of an array > > If there weren't an analytic solution to your problem, > then you could build a vector of the answers from 1 > to the maximum in the

Re: [R] Applying a function to each element of an array

2005-10-07 Thread Prof Brian Ripley
On Fri, 7 Oct 2005, Patrick Burns wrote: > If there weren't an analytic solution to your problem, > then you could build a vector of the answers from 1 > to the maximum in the matrix. Call that 'wtvec'. Then: > > ans <- array(NA, dim(A), dimnames(A)) > ans[] <- wtvec[as.vector(A)] > > should get

Re: [R] Applying a function to each element of an array

2005-10-07 Thread Patrick Burns
If there weren't an analytic solution to your problem, then you could build a vector of the answers from 1 to the maximum in the matrix. Call that 'wtvec'. Then: ans <- array(NA, dim(A), dimnames(A)) ans[] <- wtvec[as.vector(A)] should get you what you want. Patrick Burns [EMAIL PROTECTED] +4

Re: [R] Applying a function to each element of an array

2005-10-07 Thread Berton Gunter
TECTED] > Sent: Friday, October 07, 2005 12:36 PM > To: Berton Gunter > Cc: 'Tim Smith' > Subject: Re: [R] Applying a function to each element of an array > > On Fri, 7 Oct 2005, Berton Gunter wrote: > > > Well, since Sum(i=1 to i-n) =n*(n+1)/2, your loop simply gi

Re: [R] Applying a function to each element of an array

2005-10-07 Thread Prof Brian Ripley
On Fri, 7 Oct 2005, Prof Brian Ripley wrote: > If A is the matrix the answer is 2*(1 - 2^(-A)), which took about 10secs > for an example of your size. > >> From \sum_{i=1}^n x^{1-i} = (1-x^{-n})/(1-x), E & OE. NB: I have assumed n >= 1 here, as people nornally do when using 1:count. > On Fri, 7

Re: [R] Applying a function to each element of an array

2005-10-07 Thread Prof Brian Ripley
If A is the matrix the answer is 2*(1 - 2^(-A)), which took about 10secs for an example of your size. >From \sum_{i=1}^n x^{1-i} = (1-x^{-n})/(1-x), E & OE. On Fri, 7 Oct 2005, Tim Smith wrote: > I have a 7000x7000 matrix, and each element is an integer. For each > element, I want to apply the

Re: [R] Applying a function to each element of an array

2005-10-07 Thread Berton Gunter
alyze the scientific learning process." - George E. P. Box > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Tim Smith > Sent: Friday, October 07, 2005 11:48 AM > To: r-help@stat.math.ethz.ch > Subject: [R] Applying a function

Re: [R] Applying a function to each element of an array

2005-10-07 Thread Tuszynski, Jaroslaw W.
To: r-help@stat.math.ethz.ch Subject: [R] Applying a function to each element of an array Hi, I have a 7000x7000 matrix, and each element is an integer. For each element, I want to apply the function : wt <- 0 for(q in 1:count){ wt <- wt + 0.5^(q-1) } I get the value of 'count

[R] Applying a function to each element of an array

2005-10-07 Thread Tim Smith
Hi, I have a 7000x7000 matrix, and each element is an integer. For each element, I want to apply the function : wt <- 0 for(q in 1:count){ wt <- wt + 0.5^(q-1) } I get the value of 'count' from the elements in the matrix , and want to store the corresponding 'wt' value for that element.