2

Let's say I've d1, d2 and d3 as following. t is a variable where I've combined my arrays and m contains the indices of the smallest value.

>>> d1
array([[ 0.9850916 ,  0.95004463,  1.35728604,  1.18554035],
       [ 0.47624542,  0.45561795,  0.6231743 ,  0.94746001],
       [ 0.74008166,  0.        ,  1.59774065,  1.00423774],
       [ 0.86173439,  0.70940862,  1.0601817 ,  0.96112015],
       [ 1.03413477,  0.64874991,  1.27488263,  0.80250053]])
>>> d2
array([[ 0.27301946,  0.38387185,  0.93215524,  0.98851404],
       [ 0.17996978,  0.        ,  0.41283798,  0.15204035],
       [ 0.10952115,  0.45561795,  0.5334015 ,  0.75242805],
       [ 0.4600214 ,  0.74100962,  0.16743427,  0.36250385],
       [ 0.60984208,  0.35161234,  0.44580535,  0.6713633 ]])
>>> d3
array([[ 0.        ,  0.19658541,  1.14605925,  1.18431945],
       [ 0.10697428,  0.27301946,  0.45536417,  0.11922118],
       [ 0.42153386,  0.9850916 ,  0.28225364,  0.82765657],
       [ 1.04940684,  1.63082272,  0.49987388,  0.38596938],
       [ 0.21015723,  1.07007177,  0.22599987,  0.89288339]])
>>> t = np.array([d1, d2, d3])
>>> t
array([[[ 0.9850916 ,  0.95004463,  1.35728604,  1.18554035],
        [ 0.47624542,  0.45561795,  0.6231743 ,  0.94746001],
        [ 0.74008166,  0.        ,  1.59774065,  1.00423774],
        [ 0.86173439,  0.70940862,  1.0601817 ,  0.96112015],
        [ 1.03413477,  0.64874991,  1.27488263,  0.80250053]],

       [[ 0.27301946,  0.38387185,  0.93215524,  0.98851404],
        [ 0.17996978,  0.        ,  0.41283798,  0.15204035],
        [ 0.10952115,  0.45561795,  0.5334015 ,  0.75242805],
        [ 0.4600214 ,  0.74100962,  0.16743427,  0.36250385],
        [ 0.60984208,  0.35161234,  0.44580535,  0.6713633 ]],

       [[ 0.        ,  0.19658541,  1.14605925,  1.18431945],
        [ 0.10697428,  0.27301946,  0.45536417,  0.11922118],
        [ 0.42153386,  0.9850916 ,  0.28225364,  0.82765657],
        [ 1.04940684,  1.63082272,  0.49987388,  0.38596938],
        [ 0.21015723,  1.07007177,  0.22599987,  0.89288339]]])
>>> m = np.argmin(t, axis=0)
>>> m
array([[2, 2, 1, 1],
       [2, 1, 1, 2],
       [1, 0, 2, 1],
       [1, 0, 1, 1],
       [2, 1, 2, 1]])

From m and t, I want to calculate the actual values as following. How do I do this? ... preferably, the efficient way?

array([ [ 0.        ,  0.19658541,  0.93215524,  0.98851404],
        [ 0.10697428,  0.        ,  0.41283798,  0.11922118],
        [ 0.10952115,  0.        ,  0.28225364,  0.75242805],
        [ 0.4600214 ,  0.70940862,  0.16743427,  0.36250385],
        [ 0.21015723,  0.35161234,  0.22599987,  0.6713633 ]])
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
ablaze
  • 722
  • 7
  • 30

1 Answers1

1

If only the minimum is what you needed, you can use np.min(t, axis=0)

If you want to use customary indexing, you can use choose:

m.choose(t) # This will return the same thing.

It can also be written as

np.choose(m, t)

Which returns:

array([[0.        , 0.19658541, 0.93215524, 0.98851404],
       [0.10697428, 0.        , 0.41283798, 0.11922118],
       [0.10952115, 0.        , 0.28225364, 0.75242805],
       [0.4600214 , 0.70940862, 0.16743427, 0.36250385],
       [0.21015723, 0.35161234, 0.22599987, 0.6713633 ]])
Rocky Li
  • 5,641
  • 2
  • 17
  • 33