python - Minimum of Numpy Array Ignoring Diagonal -
i have find maximum value of numpy array ignoring diagonal elements.
np.amax() provides ways find ignoring specific axes. how can achieve same ignoring diagonal elements?
you use mask
mask = np.ones(a.shape, dtype=bool) np.fill_diagonal(mask, 0) max_value = a[mask].max()
where a
matrix want find max of. mask selects off-diagonal elements, a[mask]
long vector of off-diagonal elements. take max.
or, if don't mind modifying original array
np.fill_diagonal(a, -np.inf) max_value = a.max()
of course, can make copy , above without modifying original. also, assuming a
floating point format.
Comments
Post a Comment