Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6834919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:10:14+00:00 2026-05-26T23:10:14+00:00

I am having some trouble developing a suitably fast binning algorithm in Mathematica. I

  • 0

I am having some trouble developing a suitably fast binning algorithm in Mathematica. I have a large (~100k elements) data set of the form
T={{x1,y1,z1},{x2,y2,z2},….}
and I want to bin it into a 2D array of around 100×100 bins, with the bin value being given by the sum of the Z values that fall into each bin.

Currently I am iterating through each element of the table, using Select to pick out which bin it is supposed to be in based on lists of bin boundaries, and adding the z value to a list of values occupying that bin. At the end I map Total onto the list of bins, summing their contents (I do this because I sometimes want to do other things, like maximize).

I have tried using Gather and other such functions to do this but the above method was ridiculously faster, though perhaps I am using Gather poorly. Anyway It still takes a few minutes to do the sorting by my method and I feel like Mathematica can do better. Does anyone have a nice efficient algorithm handy?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:10:15+00:00Added an answer on May 26, 2026 at 11:10 pm

    Here is a method based on Szabolcs’s post that is about about an order of magnitude faster.

    data = RandomReal[5, {500000, 3}];
    (*500k values*)
    zvalues = data[[All, 3]];
    
    epsilon = 1*^-10;(*prevent 101 index*)
    (*rescale and round (x,y) coordinates to index pairs in the 1..100 range*)
    indexes = 1 + Floor[(1 - epsilon) 100 Rescale[data[[All, {1, 2}]]]];
    
    res2 = Module[{gb = GatherBy[Transpose[{indexes, zvalues}], First]}, 
        SparseArray[
         gb[[All, 1, 1]] -> 
          Total[gb[[All, All, 2]], {2}]]]; // AbsoluteTiming
    

    Gives about {2.012217, Null}

    AbsoluteTiming[
     System`SetSystemOptions[ 
      "SparseArrayOptions" -> {"TreatRepeatedEntries" -> 1}];
     res3 = SparseArray[indexes -> zvalues];
     System`SetSystemOptions[ 
      "SparseArrayOptions" -> {"TreatRepeatedEntries" -> 0}];
     ]
    

    Gives about {0.195228, Null}

    res3 == res2
    True
    

    “TreatRepeatedEntries” -> 1 adds duplicate positions up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble developing an algorithm to determine the minimum of a list
Im having some trouble writing a getstring function, this is what I have so
I am having some trouble with the Google Maps API . I have an
I'm developing a Flex application and am having some trouble working with asynchronous calls.
I am currently developing a WebGL-based application, and I am having some trouble with
Hey guys, I'm having some trouble with a screen from the app I'm developing.
I am having trouble developing some queries on the fly for our clients and
I'm having some trouble with a script confirmation. By standard it's set to hide
Having some trouble with what should be a very simple scenario. For example purposes,
Hey having some trouble trying to maintain transparency on a png when i create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.