Reputation: 3872
Another numpy array handling question: I have an approx. 2000³ entries numpy array with fixed size (that I know), containing integers. I want to pad the array with another integer, so that it's surrounded in all dimensions. This integer is fixed for the whole padding process.
example (2D)
1----->000
010
000
I have two ideas, leading to that result:
Creating a larger numpy array, containing the padded values and "slicing" the old area in the padded:
padded=np.zeros((z+2,x+2,y+2))
padded[1:z+1,1:x+1,1:y+1]=olddata
Using np.insert or hstack,vstack,dstack to add the values:
padded=np.insert(data,0,0,axis=0)
padded=np.insert(data,x+1,0,axis=0) etc.
The Problem is, that all these methods are not in-place and allocate a new array (1.) or copy the old one (2.). Is there a way to do the padding in-place? I know, that since numpy 1.7. there's the numpy.pad module. But that also seems to use some kind of allocation and overriding (like in my 1. way).
Upvotes: 2
Views: 2197
Reputation: 25207
You can't add padding in-place because there is no room for it in the memory layout. You can go the other way though: allocate the padded array first, and use a view into it when accessing unpadded data.
padded = np.empty((2002,2002,2002))
padded[0] = 0
padded[-1] = 0
padded[:,0] = 0
padded[:,-1] = 0
padded[:,:,0] = 0
padded[:,:,-1] = 0
unpadded = padded[1:-1, 1:-1, 1:-1]
Upvotes: 1