Skip to content

Commit

Permalink
mean: version that works for any iterable collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jan 8, 2013
1 parent 203f49f commit 67f7d25
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion base/statistics.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
mean(v::AbstractArray) = sum(v)/numel(v)
function mean(iterable)
state = start(iterable)
if done(iterable, state)
error("mean of empty collection undefined: $(repr(iterable))")
end
count = 1
total, state = next(iterable, state)
while !done(iterable, state)
value, state = next(iterable, state)
total += value
count += 1
end
return total/count
end
mean(v::AbstractArray, dim::Int) = sum(v,dim)/size(v,dim)

weighted_mean(v::AbstractArray, w::AbstractArray) = sum(v.*w)/sum(w)

function median(v::AbstractArray)
Expand Down

0 comments on commit 67f7d25

Please sign in to comment.