Python: Resize an existing array and fill with zeros


There is a new numpy function in version 1.7.0 numpy.pad that can do this in one-line. Like the other answers, you can construct the diagonal matrix with np.diag before the padding. The tuple ((0,N),(0,0)) used in this answer indicates the "side" of the matrix which to pad.

import numpy as np

A = np.array([1, 2, 3])

N = A.size
B = np.pad(np.diag(A), ((0,N),(0,0)), mode='constant')

B is now equal to:

[[1 0 0]
 [0 2 0]
 [0 0 3]
 [0 0 0]
 [0 0 0]
 [0 0 0]]

I think that my issue should be really simple, yet I can not find any help on the Internet whatsoever. I am very new to Python, so it is possible that I am missing something very obvious.

I have an array, S, like this [x x x] (one-dimensional). I now create a diagonal matrix, sigma, with np.diag(S) - so far, so good. Now, I want to resize this new diagonal array so that I can multiply it by another array that I have.

import numpy as np
shape = np.shape((6, 6)) #This will be some pre-determined size
sigma = np.diag(S) #diagonalise the matrix - this works
my_sigma = sigma.resize(shape) #Resize the matrix and fill with zeros - returns "None" - why?

However, when I print the contents of my_sigma, I get "None". Can someone please point me in the right direction, because I can not imagine that this should be so complicated.

Thanks in advance for any help!



I have this:

[x x x]

I want this:

[x 0 0]
[0 x 0]
[0 0 x]
[0 0 0]
[0 0 0]
[0 0 0] - or some similar size, but the diagonal elements are important.

Numpy resize and fill with specific value

Your array has a fixed size data buffer. You can reshape the array without changing that buffer. You can take a slice (view) without changing the buffer. But you can't add values to the array without changing the buffer.

In general resize returns an new array with a new data buffer.

pad is a complex function to handle general cases. But the simplest approach is to create the empty target array, fill it, and then copy the input into the right place.

Alternatively pad could create the fill arrays and concatenate them with the original. But concatenate also makes the empty return and copies.

A do it yourself pad with clipping could be structured as:

n,m = X.shape
R = np.empty((k,l))
<calc slices from n,m,k,l>
R[slice1] = X[slice2]

Calculating the slices may require if-else tests or equivalent min/max. You can probably work out those details.

This may be all that is needed


That's because there's no problem if a slice is larger than the dimension.

In [37]: np.arange(5)[:10]
Out[37]: array([0, 1, 2, 3, 4])

Thus, for example:

In [38]: X=np.ones((3,4),int)    
In [39]: R=np.empty((2,5),int)
In [40]: R.fill(9)

In [41]: R[:X.shape[0],:X.shape[1]]=X[:R.shape[0],:R.shape[1]]

In [42]: R
array([[1, 1, 1, 1, 9],
       [1, 1, 1, 1, 9]])

I can't resize numpy.ndarrays

np.resize from the docs Return a new array with the specified shape., so x_array will remain (1456, 26).

x_array = np.ones([1456, 26])
print x_array

y_array = np.ones([1456, 1])
print y_array

new_array = np.resize(x_array, y_array.shape)
print new_array

will need to assign resized array to a new array.