Element-wise array maximum function in NumPy (more than two arrays)

后端 未结 2 1210
终归单人心
终归单人心 2020-12-28 14:19

I\'m trying to return maximum values of multiple array in an element-wise comparison. For example:

A = array([0, 1, 2])
B = array([1, 0, 3])
C = array([3, 0,         


        
相关标签:
2条回答
  • 2020-12-28 14:58

    You can use reduce. It repeatedly applies a binary function to a list of values...

    For A, B and C given in question...

    np.maximum.reduce([A,B,C])
    
     array([3,1,4])
    

    It first computes the np.maximum of A and B and then computes the np.maximum of (np.maximum of A and B) and C.

    0 讨论(0)
  • 2020-12-28 15:10

    With this setup:

    >>> A = np.array([0,1,2])
    >>> B = np.array([1,0,3])
    >>> C = np.array([3,0,4])
    

    You can either do:

    >>> np.maximum.reduce([A,B,C])
    array([3, 1, 4])
    

    Or:

    >>> np.vstack([A,B,C]).max(axis=0)
    array([3, 1, 4])
    

    I would go with the first option.

    0 讨论(0)
提交回复
热议问题