5

My question is a little more specific than that actually. Consider the following arrays:

from numpy import zeros, ones

array1 = ones((3, 3), bool)
array1[0][0] = 0
array1[0][2] = 0
array1[2][0] = 0
array1[2][2] = 0

array2 = zeros((12, 12), bool)

Now what I'm looking for is a way that I can refer to a 2 dimensional portion of array2 of the same proportions as array1 so that I can add the positive values from array1 to it. I know there are ways I can do this using loops, but I'd prefer to have a single statement like array2[(some way of getting a 3x3 portion of array2)] |= array1

2 Answers 2

5

The output from an example using floats seems easier to understand:

>>> a1 = numpy.ones((3, 3))
>>> a2 = numpy.ones((12, 12))
>>> a2[:3,:3] += a1
>>> a2
array([[ 2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])

Also, note you can do things like this:

>>> a2[slice(None, a1.shape[0]), slice(None, a1.shape[1])]
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.]])
Sign up to request clarification or add additional context in comments.

Comments

2
array2[start:end,start:end] |= array1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.