An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20051007/507f9db3/attachment.pl
Applying a function to each element of an array
8 messages · Tim Smith, Bert Gunter, Kjetil Holuerson +3 more
Well, since Sum(i=1 to i-n) =n*(n+1)/2, your loop simply gives 1/4*count*(count-1). So if your matrix is A, A*(A-1)/4 is about the quickest way to get your answer I think. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Tim Smith
Sent: Friday, October 07, 2005 11:48 AM
To: r-help at 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' from the elements in the matrix ,
and want to store the corresponding 'wt' value for that element.
I suppose I could loop through the matrix, and apply the
function to each element but this would take a really really
long time. Are there any quicker ways to get the same result?
many thanks,
Tim
---------------------------------
[[alternative HTML version deleted]]
______________________________________________ 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
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))
I get the value of 'count' from the elements in the matrix , and want to store the corresponding 'wt' value for that element. I suppose I could loop through the matrix, and apply the function to each element but this would take a really really long time. Are there any quicker ways to get the same result? many thanks,
mapply(helper, yourmat) gives you the elements of the matrix, as a vector. So you only must reassemble as a matrix: n <- nrow(yourmat) p <- ncol(yourmat) matrix( mapply(helper, yourmat), n, p) Kjetil
Tim --------------------------------- [[alternative HTML version deleted]]
______________________________________________ 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
--
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 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.
I suppose I could loop through the matrix, and apply the function to
each element but this would take a really really long time. Are there
any quicker ways to get the same result?
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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 Oct 2005, Tim Smith wrote:
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.
I suppose I could loop through the matrix, and apply the function to
each element but this would take a really really long time. Are there
any quicker ways to get the same result?
-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
______________________________________________ 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
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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 patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User")
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)
}
I get the value of 'count' from the elements in the matrix , and want to store the corresponding 'wt' value for that element.
I suppose I could loop through the matrix, and apply the function to each element but this would take a really really long time. Are there any quicker ways to get the same result?
many thanks,
Tim
---------------------------------
[[alternative HTML version deleted]]
______________________________________________ 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
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 you what you want.
Well, we don't actually know 'count' is >= 1. I almost suggested this, but suppose the counts were in billions? It's a good solution for a small set of continguous values. If there is a small set of values, do a two-step lookup using e.g. match to find the enumeration of the values.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
The sum in the loop is simply: 2 - (0.5)^count. So you don't need this loop. As "count" gets large, the sum approaches 2. Ravi.
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help- bounces at stat.math.ethz.ch] On Behalf Of Patrick Burns Sent: Friday, October 07, 2005 4:29 PM To: Tim Smith Cc: r-help at 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 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 patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") 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)
}
I get the value of 'count' from the elements in the matrix , and want to
store the corresponding 'wt' value for that element.
I suppose I could loop through the matrix, and apply the function to each
element but this would take a really really long time. Are there any quicker ways to get the same result?
many thanks, Tim --------------------------------- [[alternative HTML version deleted]]
______________________________________________ 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
______________________________________________ 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