Zadanie:
Opis
Jako rozwiązanie kod:
def reduce(matIn, n_size):
'''
Function which reduces the size of matrix. Elements values are
being calculated as average of neighbours
:param matIn: bigger input matrix
:param n_size: new size, has to be smaller
:return:
'''
if isinstance(n_size,tuple) or isinstance(n_size, list):
nx, ny = n_size
elif isinstance(n_size,int):
nx = n_size
ny = nx
else:
raise Exception("New size should be tuple/list for nonsqure matrix or integer for squre matrix")
sx = len(matIn)
sy = len(matIn[0])
if sx < nx or sy < ny:
raise Exception("New size should be smaller then origin")
nxs = sx / nx
nys = sy / nx
mat = [[0. for i in range(nx)] for j in range(ny)]
for i in range(nx):
for j in range(ny):
x = int(i*nxs)
y = int(j*nys)
ids = 1
s = matIn[x][y]
for xi in range(-1, 2, 2):
for yj in range(-1, 2, 2):
ii = x+xi
jj = y + yj
if 0 <= ii < sx and 0 <= jj < sy:
s += matIn[x+xi][y+yj]
ids += 1
mat[i][j] = s/ids
return mat
Tylko, że nie ma żadnych wyników. Jak to przetestować?