Better (non-linear) binning

后端 未结 2 365
南笙
南笙 2021-01-15 10:57

The last question I asked concerned how to bin data by x co-ordinate. The solution was simple and elegant, and I\'m ashamed I didn\'t see it. This question may be harder (

2条回答
  •  一整个雨季
    2021-01-15 11:18

    I've never used matlab but from looking at your previous question I suspect your looking for something along the lines of a Kdtree or a variation.

    Clarification: Since there seems to be some confusion about this I think an pseudocode example is in order.

    // Some of this shamelessly borrowed from the wikipedia article
    function kdtree(points, lower_bound, upper_bound) {
        // lower_bound and upper_bound are the boundaries of your bucket
        if(points is empty) {
            return nil
        }
        // It's a trivial exercise to control the minimum size of a partition as well
        else {
            // Sort the points list and choose the median element
            select median from points.x
    
            node.location = median;
    
            node.left = kdtree(select from points where lower_bound < points.x <= median, lower_bound, median);
            node.right = kdtree(select from points where median < points.x <= upper_bound, median, upper_bound);
    
            return node
        }
    }
    
    kdtree(points, -inf, inf)
    
    // or alternatively
    
    kdtree(points, min(points.x), max(points.x))
    

提交回复
热议问题