Finding coordinates of min/max attribute values?


#1

Hi,

is there a way to easily find the dimension coordinates of some aggregate value of an attribute?

Let’s say I want to find the maximum value of attribute x and the coordinates i and j where this maximum is located - how would I do this?


#2

Here is one way to go:

Assuming a two-dimensional array “test”, with an attribute “d”, you can find the coordinates of the min value like this:

filter(
    cross_join(
        test,
        aggregate(
            test,
            min(d)
        )
    ),
    d = d_min
)

The idea is to find the min, use cross join to compare it to the current values of the array, and use filter to find the location where the min matches.

Anybody else have a more efficient way they like to do this?

–Steve F
Paradigm4


#3

I tried out a few approaches for finding the minimum coordinates to see how they perform.
My (non-sparse) test array looks like this:
gridlat:float,lon:float [gpi=0:3264390,250000,0]

First, I compute a distance measure to a point given by ($LAT, $LON) and search the minimum distance in the array:

aggregate(apply(grid, dist, pow(lat - $LAT, 2) + pow(lon - $LON, 2)), min(dist));

(Please ignore that euclidean distance makes no sense here, I’m just using it as a placeholder for “some distance computation”)
On my system, running iquery with that query and timing it with linux “time” gives something in the order of magnitude of 0.4s.

I then tried your query, like so:

filter(
    cross_join(
        aggregate(
            apply(grid, dist, pow(lat - $LAT, 2) + pow(lon - $LON, 2)),
            min(dist) as mindist
        ),
        apply(grid, dist, pow(lat - $LAT, 2) + pow(lon - $LON, 2))
    ),
    dist=mindist
)

This takes in the order of magnitude of 30s.

As an alternative, I tried sorting the whole array according to the distance measure attribute and taking only the first value of that sorted array:

subarray(sort(apply(grid, dist, pow(lat - $LAT, 2) + pow(lon - $LON, 2), gpi_val, gpi), dist), 0, 2);

That takes about 10s.

So it seems to me that using the filter approach is not the most performant possiblity.

I’ll try implementing my own minimum_coordinate() operator by using the source for the min() operator as a basis. If I come up with something useful, I’ll post it here.