How do I make a subset of matrix equal to something

Viewed 44

I got a big matrix with size (1200,1200) full of zeros and want to make a chunk of that matrix equal to smaller one of size(120,120) something like

big[0:120, 0:120]=small
1 Answers

Why, big[0:120, 0:120]=small is precisely what you need. That's what broadcasting does. Consider a smaller example that plants a 3x3 matrix of 1s into the upper left corner of a bigger matrix:

big = np.arange(25).reshape(5, 5)
small = np.ones((3, 3))
big[:3, :3] = small
#Or, rather: big[:small.shape[0], :small.shape[1]] = small
big
#array([[ 1,  1,  1,  3,  4],
#       [ 1,  1,  1,  8,  9],
#       [ 1,  1,  1, 13, 14],
#       [15, 16, 17, 18, 19],
#       [20, 21, 22, 23, 24]])
Related