Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - increase array size and initialize new elements to zero

Tags:

python

arrays

I have an array of a size 2 x 2 and I want to change the size to 3 x 4.

A = [[1 2 ],[2 3]]
A_new = [[1 2 0 0],[2 3 0 0],[0 0 0 0]]

I tried 3 shape but it didn't and append can only append row, not column. I don't want to iterate through each row to add the column.

Is there any vectorized way to do this like that of in MATLAB: A(:,3:4) = 0; and A(3,:) = 0; this converted the A from 2 x 2 to 3 x 4. I was thinking is there a similar way in python?

like image 761
yashgarg1232 Avatar asked Dec 14 '22 14:12

yashgarg1232


2 Answers

In Python, if the input is a numpy array, you can use np.lib.pad to pad zeros around it -

import numpy as np

A = np.array([[1, 2 ],[2, 3]])   # Input
A_new = np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0)) # Output

Sample run -

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array

If you don't want to do the math of how many zeros to pad, you can let the code do it for you given the output array size -

In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])

Or, you can start off with a zero initialized output array and then put back those input elements from A -

In [38]: A
Out[38]: 
array([[1, 2],
       [2, 3]])

In [39]: A_new = np.zeros(new_shape,dtype = A.dtype)

In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A

In [41]: A_new
Out[41]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])

In MATLAB, you can use padarray -

A_new  = padarray(A,[1 2],'post')

Sample run -

>> A
A =
     1     2
     2     3
>> A_new = padarray(A,[1 2],'post')
A_new =
     1     2     0     0
     2     3     0     0
     0     0     0     0
like image 138
Divakar Avatar answered May 09 '23 11:05

Divakar


Pure Python way achieve this:

row = 3
column = 4
A = [[1, 2],[2, 3]]

A_new = map(lambda x: x + ([0] * (column - len(x))), A + ([[0] * column] * (row - len(A))))

then A_new is [[1, 2, 0, 0], [2, 3, 0, 0], [0, 0, 0, 0]].

Good to know:

  • [x] * n will repeat x n-times
  • Lists can be concatenated using the + operator

Explanation:

  • map(function, list) will iterate each item in list pass it to function and replace that item with the return value
  • A + ([[0] * column] * (row - len(A))): A is being extended with the remaining "zeroed" lists
    • repeat the item in [0] by the column count
    • repeat that array by the remaining row count
  • ([0] * (column - len(x))): for each row item (x) add an list with the remaining count of columns using
like image 30
liushuaikobe Avatar answered May 09 '23 09:05

liushuaikobe