I have a problem with lists/arrays/matrix at Python.
I have a list of matrix (or arrays if it need to be) and i want to add to every single of of them a new column of ones (of the same number of lines). How can I do this??
I’ve a couple of things and didn’t get any success.
Thanks for the help.
Here’s an example:
>>> A=[mat([[1,2,3],[4,5,6],[7,8,9]]),mat([[1,0,0],[0,1,0],[0,0,1]])]
>>> A
[matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]), matrix([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])]
Using the answer you guys told
>>> A = np.hstack((A, np.ones((A.shape[0],1),dtype=A.type)))
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
A = np.hstack((A, np.ones((A.shape[0],1),dtype=A.type)))
AttributeError: 'list' object has no attribute 'shape'`
Example for a 2D NumPy ndarray:
Edit: It works the same for a matrix. For a list of matrices, you can use a for loop: