Skip to main content
1 of 6
kglr
  • 400.5k
  • 18
  • 488
  • 929

Modifying @ruebenko's answer in the StackOverflow Q/A linked in Faysal's comment (Mathematica fast 2D binning algorithm) to get the means of z-values for each bin (using yet another undocumented setting for the option "TreatRepeatedEntries" ):

 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]

enter image description here

Timing:

 Timing[System`SetSystemOptions["SparseArrayOptions" ->
    {"TreatRepeatedEntries" -> (Mean[{#}] &)}]; 
 binmeansZ = SparseArray[indexes -> zvalues];
 System`SetSystemOptions[
   "SparseArrayOptions" -> {"TreatRepeatedEntries" -> First}];]
 (* {0.031200, Null} *)
kglr
  • 400.5k
  • 18
  • 488
  • 929