Is there a slick way to rewrite this Julia function, perhaps using just 1 line of code, without making it much slower? (I just started using Julia. It\'s great!) K
Any alternative probably will not be faster. Your loop already does only one pass through the array. Julia loops are fast, and there is no speed advantage to vectorized code, as there is in other languages.
Have a look at Julia's implementation of the hist function. This is taken directly from the Julia Standard Library:
function hist(v::AbstractVector, edg::AbstractVector)
n = length(edg)-1
h = zeros(Int, n)
for x in v
i = searchsortedfirst(edg, x)-1
if 1 <= i <= n
h[i] += 1
end
end
edg,h
end
The "edg" parameter contains the edges of the bins. If we remove that feature, we get exactly the function you wrote.
EDIT hist has not been available in Julia Base since v0.5.0