Skip to content

Instantly share code, notes, and snippets.

@akhalsa
Created January 9, 2017 02:20
Show Gist options
  • Save akhalsa/5a1bd3ae8a0a1e6674a8d7a827ac04b9 to your computer and use it in GitHub Desktop.
Save akhalsa/5a1bd3ae8a0a1e6674a8d7a827ac04b9 to your computer and use it in GitHub Desktop.
import numpy as np
# Create the following rank 2 array with shape (3, 4)
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
# [6 7]]
b = a[:2, 1:3]
# A slice of an array is a view into the same data, so modifying it
# will modify the original array.
print a[0, 1] # Prints "2"
b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
print a[0, 1] # Prints "77"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment