1

i have a large array and want to place a smaller array at some offset and at the same time ignore zeros in the smaller array. what's the best way to do that?

I tried masked arrays but somehow this is slow

mask = np.ma.masked_equal(pixels, 0, False)
output[offset_y:offset_y+tile_height,offset_x:offset_x+tile_width] = np.where(mask.mask, output[offset_y:offset_y+tile_height,offset_x:offset_x+tile_width], pixels)

then i tried this

np.place(output[offset_y:offset_y+tile_height,offset_x:offset_x+tile_width], pixels>0, pixels[pixels>0])

but it is very slow

what is the fastest way to do so?

7
  • Create a new empty array of the right size, and copy values to it from both the original and new arrays. I'd try to remove the zeros from the smaller array first. Commented Jan 26, 2015 at 22:44
  • Give us some idea of the size and shape of the arrays you are dealing with. Better yet, give us some small small arrays to test. Commented Jan 26, 2015 at 22:44
  • the size of output is 30000x50000 and the pixels are 16000x16000.. Commented Jan 26, 2015 at 22:45
  • i think it should be fast with masked arrays but i am not sure why it does not work Commented Jan 26, 2015 at 22:46
  • just updated the masked array example to a working one Commented Jan 26, 2015 at 23:07

1 Answer 1

2

I suspect regular old logical indexing will be the most efficient here:

# make a view into output with the same dimensions as `pixels`
output_subarray = output[offset_y:offset_y+tile_height,offset_x:offset_x+tile_width]

mask = pixels != 0
output_subarray[mask] = pixels[mask]
Sign up to request clarification or add additional context in comments.

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.