I need to append boundaries to my matrix, it's just a repetition of first column and row on the beginning and last column and row at the end of matrix.
I have this PoC:
matrix = np.arange(20).reshape(4,5)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
And when I insert rows at the top and bottom like this, it works fine.
shape = matrix.shape (4,5)
matrix_t = np.insert(matrix, [0, shape[0]], [matrix[0], matrix[shape[0]-1]], axis=0)
[[ 0 1 2 3 4]
[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[15 16 17 18 19]]
As you can see it has added 0 1 2 3 4 as first row and 15 16 17 18 19 as last.
Now I wanted to do same thing just to append columns on the left and on the right. Simplifying above code a bit, I did it like this (needed to reshape to create column vector).
temp1 = np.arange(4).reshape(4,1)
temp2 = np.arange(4, 8, 1).reshape(4,1)
matrix_t = np.insert(matrix, [0, 5], [temp1, temp2], axis=1)
And then i got this error:
Traceback (most recent call last):
File "main.py", line 33, in <module>
matrix_t = np.insert(matrix, [0, 5], [temp1, temp2], axis=1)
File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 3496, in insert
new[slobj] = values
ValueError: total size of new array must be unchanged
When i do it like this, it works perfectly fine:
matrix_t = np.insert(matrix, [0, 5], temp1, axis=1)
[[ 0 0 1 2 3 4 0]
[ 1 5 6 7 8 9 1]
[ 2 10 11 12 13 14 2]
[ 3 15 16 17 18 19 3]]
What am I missing?