Skip to content

Instantly share code, notes, and snippets.

@neogeogre
Created May 26, 2020 08:51
Show Gist options
  • Save neogeogre/d82a2714b8bc27cb05ed893289e8889f to your computer and use it in GitHub Desktop.
Save neogeogre/d82a2714b8bc27cb05ed893289e8889f to your computer and use it in GitHub Desktop.
two dimentional interpolation in matrix values
from scipy.interpolate import interp2d
def matrix_interp():
mat = numpy.array([[70.45, 0, 21.78, 0],
[0, 0, 0, 0],
[105.12, 0, 1000.45, 0],
[0, 0, 0, 0]])
ix = numpy.where(mat != 0)
f = interp2d(ix[0], ix[1], mat[ix].flatten(), kind='linear')
mat2 = mat.copy()
mat2[mat == 0] = f(range(4), range(4)).T[mat == 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment