Просто заполните матрицы соответствующими столбцами нулей, затем добавьте как обычно.
import numpy as np
# define inputs and desired output
x = np.array([[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]])
y = np.array([[7,7,7,7],[7,3,3,7],[7,3,3,7],[7,7,7,7]])
f = np.array([[1,1,8,8,7,7],[1,2,9,4,3,7],[1,2,9,4,3,7],[1,1,8,8,7,7]])
shape = (x.shape[0],x.shape[1]+2) # the shape of the desired output
xpad = np.zeros(shape) # pad the x array on the left with two columns of 0
ypad = np.zeros(shape) # and pad the y array on the right with two columns
xpad[:,:-2]=x # like this
ypad[:,2:]=y
f_ = xpad+ypad # and then do the addition to get f_ the actual output
np.alltrue(f_==f) #True -- it works
Комментарий: вы, вероятно, не хотите np.matrix
. Я думаю, что они собираются обесценить это. Для большинства целей он ничего не делает np.array
не может сделать в целом.
другой способ, который труднее читать, это
f_ = np.pad(x,((0,0),(0,2)),'constant')+np.pad(y,((0,0),(2,0)),'constant')
Вы можете легко распространить это на ваш случай. Дополните каждый массив нулями до нужной формы, а затем сложите его