Frederico Schardong
Frederico Schardong

Reputation: 2105

Copy a ndarray into another ndarray at a given position

In numpy how can I copy a 2d array into another 2d array at a given x and y coordinate?

For example, I have this 7 by 7 array of zeros:

0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0

And I want to place this 2 by 2 array of ones on row 2 and column 3 of the previous array, resulting in:

0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 1, 1, 0, 0
0, 0, 0, 1, 1, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0

Upvotes: 1

Views: 99

Answers (1)

Daniel
Daniel

Reputation: 42768

With item-slice setting:

A = numpy.zeros((7,7))
B = numpy.ones((2,2))
A[2:4, 3:5] = B

Upvotes: 2

Related Questions