python - Inserting one item on a matrix -
how insert single element array on numpy. know how insert entire column or row using insert , axis parameter. how insert/expand one.
for example, have array:
1 1 1 1 1 1 1 1 1
how insert 0 (on same row), on (1, 1) location, say:
1 1 1 1 0 1 1 1 1 1
is doable? if so, how do opposite (on same column), say:
1 1 1 1 0 1 1 1 1 1
numpy has looks ragged arrays, arrays of objects, , not want. note difference in following:
in [27]: np.array([[1, 2], [3]]) out[27]: array([[1, 2], [3]], dtype=object) in [28]: np.array([[1, 2], [3, 4]]) out[28]: array([[1, 2], [3, 4]])
if want insert v
row/column i/j
, can padding other rows. easy do:
in [29]: = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) in [30]: i, j, v = 1, 1, 3 in [31]: np.array([np.append(a[i_], [0]) if i_ != else np.insert(a[i_], j, v) i_ in range(a.shape[1])]) out[31]: array([[1, 1, 1, 0], [1, 3, 1, 1], [1, 1, 1, 0]])
to pad along columns, not rows, first transpose a
, perform operation, transpose again.
Comments
Post a Comment