A
A
Andrey Enkin2015-04-27 07:01:47
PHP
Andrey Enkin, 2015-04-27 07:01:47

PHP: how to group elements of an array that are close in value?

There is an array like:

Array
(
    [0] => Array
        (
            [posY] => 5
            [posX] => 48.75
            [id] => 36
        )

    [1] => Array
        (
            [posY] => 5
            [posX] => 52.5
            [id] => 35
        )

    [2] => Array
        (
            [posY] => 5.5
            [posX] => 18.75
            [id] => 46
        )

    [3] => Array
        (
            [posY] => 7.5
            [posX] => 52.5
            [id] => 29
        )

    [4] => Array
        (
            [posY] => 15
            [posX] => 45
            [id] => 49
        )

    [5] => Array
        (
            [posY] => 16
            [posX] => 75
            [id] => 15
        )
)

It is necessary to group the elements of the array according to the condition: posX && posY in the group differ each from each other by no more than +\- some value.
These are X and Y coordinates.
In other words, elements with close both coordinates must be combined into a group. The degree of proximity for each coordinate is set separately (X +\- 5, Y +\- 10 , for example)
At the output, you need to get an array:
Array 
(
    [1] => Array
        (
            [0] => Array
                (
                    [posY] => 5
                    [posX] => 48.75
                    [id] => 36
                )
        )

    [2] => Array
        (
            [0] => Array
                (
                    [posY] => 5
                    [posX] => 52.5
                    [id] => 35
                )

            [1] => Array
                (
                    [posY] => 5.5
                    [posX] => 18.75
                    [id] => 46
                )

            [2] => Array
                (
                    [posY] => 7.5
                    [posX] => 52.5
                    [id] => 29
                )
        )

    [3] => Array
        ( 
            [0] => Array
                (
                    [posY] => 15
                    [posX] => 45
                    [id] => 49
                )

             [1] => Array
                (
                    [posY] => 16
                    [posX] => 75
                    [id] => 15
                ) 
        )
)

It is impossible to do through a fixed number of groups with predefined ranges, it is necessary to check according to the proximity criterion and combine it into a group.
UPD: I will explain the problem. There is a two-dimensional scheme. There are objects with coordinates. X is the derivative of the size. Y is the derivative of the diving depth of the object. I need to group objects that dive to the same depth and similar size into groups.
934ec49467ad44f8ae1a7501c2c05889.png
Help with an algorithm?
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mrrl, 2015-04-27
@Mrl

An incomprehensible task.
First, there can be many solutions. For example, let borders be abs(dx) <= 5, abs(dy) <= 5. Points A=(0,0), B=(4,4), C=(7,7). Then the solution will be both {A,B},{C} and {A},{B,C}. Is one of them correct, or both?
What if the points A=(0.0), B=(4.4), C=(7.7), D=(11.11)? Is the solution {A},{B,C},{D} good? It has more groups than {A,B},{C,D}, but the central group is denser.
And finally, is the condition "posX && posY in a group different from each other by no more than +\- some value" the only one? If yes, it is enough to create N groups, one element in each. The condition will be met. And if not - try to explain why this particular solution is not suitable. State the condition that is violated. Maybe then it will become clearer what kind of task it is. In the meantime, this is just a wish "make me beautiful."
And if the task is not formalized - look for the word "clustering". A lot has been written about her.

X
xmoonlight, 2015-04-27
@xmoonlight

deltaX=abs(posX0-posX1)
deltaY=abs(posY0-posY1)
further - bring the functions to your code according to the structure.

D
Denis, 2015-04-28
@prototype_denis

Have you tried k-means ?
https://github.com/JCowdy/K-Means-PHP/blob/master/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question