In [ ]:
import numpy as np
def parseMatStr(s, n):
mat = np.empty((n, n))
for i, line in enumerate(s.strip().split("\n")):
mat[i,:] = np.asarray(list(map(int, line.split())))
return mat
In [ ]:
matStr = """
7 53 183 439 863
497 383 563 79 973
287 63 343 169 583
627 343 773 959 943
767 473 103 699 303
"""
mat = parseMatStr(matStr, 5)
In [ ]:
def _computeScoreMatrix(m,):
scores = np.empty_like(m)
norm = m.shape[0]# * m.shape[1]
for x in range(m.shape[0]):
for y in range(m.shape[1]):
if np.isinf(m[x, y]):
scores[x, y] = -np.inf
continue
# Compute total sum of 'blocked' values
scores[x, y] = (-(np.sum(m[:,y]) + np.sum(m[x,:]) - 2*m[x, y]) / 1) + m[x, y]
#print(mat2[x,y], mat[x,y])
return scores
def computeScoreMatrix(mat):
# Compute, for each cell, the score of that cell (high = good)
scores = _computeScoreMatrix(mat)
# Compute the score matrix_computeScoreMatrix
#return _computeScoreMatrix(scores)
return scores
def doStuff(mat):
m = mat.copy()
# Find maximum score
m2 = computeScoreMatrix(m).copy()
for i in range(5):
idxs = np.unravel_index(np.argmax(m2), m2.shape)
print(idxs[0], idxs[1], mat[idxs[0], idxs[1]])
#m2[idxs[0], idxs[1]] = -np.inf
# Erase section in matrix
m2[:,idxs[1]] = -np.inf
m2[idxs[0],:] = -np.inf
doStuff(mat)
In [ ]:
np.unravel_index(np.argmax(m2), m2.shape)
In [ ]:
mat[4, 0]
In [ ]: