简体   繁体   中英

How to find an array with the min values of multiple arrays with the same length

I have a multidimensional gridded array with dimensions of (29,320,180), where 29 is the number of array, 320 is the latidutal value and 180 is the longitudal value. I want to find the min value at every grid point out of all 29 arrays, so finally i can have an array with dimensions of 320x180 consisting of the minimum value at each grid point. I have to undermine that every array has a large number of nan values. How can i achieve that? For example two arrays with same dimensions: a=[[1,2,3],[3,5,8],[4,8,12]] b=[[3,5,6],[9,12,5],[5,6,14]] and the wanted output will be an array with the min value at each index, meaning: c=[[1,2,3],[3,5,5],[4,6,12]]

I wasn't sure if you needed the minimum of each array in terms of columns or rows, you can choose which one you want with the example below.

Let's create an example of several small 2D arrays:

import numpy as np
ex_dict = {}
lat_min = []
lon_min = []
# creating fake data assuming instead of the 29 arrays of dimensions 320x180 you have 5 arrays of dimensions 2x5 (so we can see the output) and all the arrays are stored in a dictionnary (because it's easier for me to randomly create them that way :)
for i in range(0,5):
    ex_dict[i] = np.stack([np.random.choice(range(i,20), 5, replace=False) for _ in range(2)])

Let's look at our arrays:

ex_dict
{0: array([[19, 18,  5, 13,  6],
        [ 5, 12,  3,  8,  0]]),
 1: array([[10, 13,  2, 19, 15],
        [ 5, 19,  6,  8, 14]]),
 2: array([[ 5, 17, 10, 11,  7],
        [19,  2, 11,  5,  6]]),
 3: array([[14,  3, 17,  4, 11],
        [18, 10,  8,  3,  7]]),
 4: array([[15,  8, 18, 14, 10],
        [ 5, 19, 12, 16, 13]])}

Then let's create a list to store the minimum values for each array (lat_min contains the minimum for each raw and lat_lon for each column through all the arrays):

# for each of the 5 arrays (in this example, stored in the ex_dict dictionnary), find the minimum in each row (axis = 1) and each column (axis = 2)
for i in ex_dict:
    lat_min.append(np.nanmin(ex_dict[i], axis=1))
    lon_min.append(np.nanmin(ex_dict[i], axis=0)) 

Our lists with minimum values:

lat_min
[array([5, 0]), array([2, 5]), array([5, 2]), array([3, 3]), array([8, 5])]

lon_min
[array([ 5, 12,  3,  8,  0]),
 array([ 5, 13,  2,  8, 14]),
 array([ 5,  2, 10,  5,  6]),
 array([14,  3,  8,  3,  7]),
 array([ 5,  8, 12, 14, 10])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM