# 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

#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);``