Your original code, as a function of 'm' and 'bins' is
f0 - function (m, bins) {
t(apply(m, 1, function(x) hist(x, breaks = bins, plot = FALSE)$counts))
}
and the time it takes to run on your m1 is about 5 s. on my machine
system.time(r0 - f0(m1,bins))
user system elapsed
4.950.00
And since as.integer(cut(x,bins)) is essentially findInterval(x,bins)
(since we throw away the labels made by cut()), I tried using
findInterval instead of cut() and it cut the time by more than half,
so your 5.0 s. is now c. 0.1 s.
f3 - function (m, bins)
{
nbins - length(bins) - 1L
m -
This works great, thanks a lot!
-AOB
-Original Message-
From: William Dunlap [mailto:wdun...@tibco.com]
Sent: Friday, May 02, 2014 12:31 PM
To: Ortiz-Bobea, Ariel
Cc: r-help@r-project.org
Subject: Re: [R] speeding up applying hist() over rows of a matrix
And since as.integer(cut(x,bins
Hello everyone,
I'm trying to construct bins for each row in a matrix. I'm using apply() in
combination with hist() to do this. Performing this binning for a 10K-by-50
matrix takes about 5 seconds, but only 0.5 seconds for a 1K-by-500 matrix. This
suggests the bottleneck is accessing rows in
4 matches
Mail list logo