Skip to content

Instantly share code, notes, and snippets.

@mintisan
Created March 27, 2018 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mintisan/aed0da31f6d68c0f0f480998c1cacdd1 to your computer and use it in GitHub Desktop.
Save mintisan/aed0da31f6d68c0f0f480998c1cacdd1 to your computer and use it in GitHub Desktop.
Apply Operations To Elements with Lambda
# Create matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Create a function that adds 100 to something
add_100 = lambda i: i + 100
# Create a vectorized function
vectorized_add_100 = np.vectorize(add_100)
# Apply function to all elements in matrix
vectorized_add_100(matrix)
# array([[101, 102, 103],
# [104, 105, 106],
# [107, 108, 109]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment