Modifying @ruebenko's answer in the StackOverflow
Q/A linked in Faysal's comment
(Mathematica fast 2D binning algorithm)(Mathematica fast 2D binning algorithm) to get the means of z
-values for each bin (using yet another undocumented setting for the option "TreatRepeatedEntries"
that works in version 9 only):
zvalues = data[[All, 3]];
epsilon = 1*^-10;
indexes = 1 + Floor[(1 - epsilon) 256 Rescale[data[[All, {1, 2}]]]];
System`SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> (Mean[{##}] &)}];
binmeansZ = SparseArray[indexes -> zvalues];
System`SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> First}];
A picture:
MatrixPlot[binmeansZ]
Update: Timings
Mr.Wizards's version 7 settings (also works in versions 8.0.4.0 and 9):
SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> 2}];
AbsoluteTiming[binmeans = Normal[SparseArray[indexes -> zvalues]] /.
"List"[x__] :> Mean@{x};]
SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> 0}];
(* {0.086009, Null} *)
Version 9 settings:
System`SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> (Mean[{##}] &)}];
AbsoluteTiming[binmeansZ = SparseArray[indexes -> zvalues];]
System`SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> First}];
(* {0.035003, Null}*)
binmeansZ == SparseArray[binmeans]
(* True *)
Update 2: Default settings in versions 8.0.4.0 and 9:
"TreatRepeatedEntries" /. SystemOptions["SparseArrayOptions"][[1, 2]]
(* 0 (Version 8.0.4.0) *)
(* First (Version 9) *)