I have a numpy multidimensional array array with the following format:
a = [[1,2],
[8,9]]
Then I want to add a list with 3 values (e.g. [4,5,6]
at the end horizontally and vertically with the following result:
a = [[1,2,4],
[8,9,5],
[4,5,6]]
Do I need to combine row_stack and column_stack somehow?
Here is a way using hstack
and vstack
:
>>> a = [[1,2],
... [8,9]]
>>> x = np.array([4, 5, 6])
>>> b = np.vstack((a, x[:-1]))
>>> print np.hstack((b, x[:, None]))
[[1 2 4]
[8 9 5]
[4 5 6]]
You can combine this into one line:
>>> print np.hstack((np.vstack((a, x[:-1])), x[:, None]))
[[1 2 4]
[8 9 5]
[4 5 6]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With