Finding numbers dominating the rows and columns of a matrix

import numpy as np import numpy.ma as ma import matplotlib.pyplot as plt np.random.seed(10) from_val, to_val = 1, 100 rows, cols = 10, 10 strike_rows, strike_cols = [], [] items = rows * cols colorbar_labels = 16 A = np.arange(items) np.random.shuffle(A) A = A.reshape(rows, cols) M = ma.zeros((rows, cols), dtype=int) print('A\n%s' % (A)) while len(strike_rows) != rows and len(strike_cols) != cols: max_row, max_col = divmod(np.argmax(A), cols) if max_row in strike_rows or max_col in strike_cols: A[max_row, max_col] = 0 continue M[max_row, max_col] = A[max_row, max_col] A[max_row, max_col] = 0 strike_rows.append(max_row) strike_cols.append(max_col) M = ma.masked_where(M == 0, M) print('M\n%s' % (M)) fig, ax = plt.subplots(figsize=(6, 6)) ax.set_title('Matrix M') mat = ax.matshow(M) ax.set_xticks([v+0.5 for v in range(cols)]) ax.set_yticks([v+0.5 for v in range(rows)]) ax.xaxis.set_ticks_position('bottom') plt.tick_params(axis='both', which='both', labelleft=False, labelbottom=False) plt.grid(lw=0.4, color='#DDDDDD') plt.colorbar(mat, orientation='horizontal', shrink=0.5, ticks=np.linspace(1, items + 1, colorbar_labels, dtype=M.dtype)) plt.tight_layout() plt.show() Unique numbers dominating the rows and columns of a matrix Visualization of the masked matrix holding the dominating numbers