Home > Software engineering >  How to insert extra rows and columns at desired location in a matrix ( python )?
How to insert extra rows and columns at desired location in a matrix ( python )?

Time:04-12

I was running a loop, from where I obtained ( 4 by 4) arrays, and now I want to insert zeros at certain locations in these arrays and turn them into (6 by 6) arrays.

I want to add zeros in third and sixth row , and also 3rd and sixth column

a_obtained = np.array( [[1,     2,   3,    4   ],
                        [5,     6,   7,    8   ],
                        [9,    10,   11,  12   ],
                        [13,   14,   15,   16  ]])

I tried going through np.vstack, np.hstack, but I am not able to place the zeros at specified locations.

a_desired = result = np.array([[1,     2,  0,   3,    4,   0 ],
                               [5,     6,  0,   7,    8,   0 ],
                               [0,     0,  0,   0,    0,   0 ],
                               [9,    10,  0,   11,  12,   0 ],
                               [13,   14,  0,   15,   6,   0 ],
                               [0,     0,  0,   0,    0,   0 ]])

CodePudding user response:

If we have the desired columns indices, it can be done by np.insert as:

desired_cols_ids = np.array([2, 4], dtype=np.int64)

zero_arr_row = np.zeros((1, a_obtained.shape[1]))
# [[0. 0. 0. 0.]]

a_obtained = np.insert(a_obtained, desired_cols_ids, zero_arr_row, axis=0)
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 0  0  0  0]
#  [ 9 10 11 12]
#  [13 14 15 16]
#  [ 0  0  0  0]]

zero_arr_col = np.zeros((a_obtained.shape[0], 1))
# [[0.]
#  [0.]
#  [0.]
#  [0.]
#  [0.]
#  [0.]]

a_obtained = np.insert(a_obtained, desired_cols_ids, zero_arr_col, axis=1)
# [[ 1  2  0  3  4  0]
#  [ 5  6  0  7  8  0]
#  [ 0  0  0  0  0  0]
#  [ 9 10  0 11 12  0]
#  [13 14  0 15 16  0]
#  [ 0  0  0  0  0  0]]

For a little explanation, at first, we insert into the array rows (so it is modified) and then insert into the columns of the modified array.

CodePudding user response:

import numpy as np
a_obtained = np.array( [[1,     2,   3,    4   ],
                        [5,     6,   7,    8   ],
                        [9,    10,   11,  12   ],
                        [13,   14,   15,   16  ]])
                        
new_row = np.zeros(4)
a_obtained = np.insert(a_obtained, [3, 4], [new_row], axis= 0)
print(a_obtained)

Refer: https://numpy.org/doc/stable/reference/generated/numpy.insert.html for detail explanation about insert

  • Related