Convert a 2D Numpy Array to a 2D Numpy Matrix
I Have a Python Code in Which I Have to Convert a 2D Array to a 2D Matrix So That I Can Use It to Calculate Inverse. for That I Am Using Numpy. Matrix(Array)...
I have a python code in which I have to convert a 2D array to a 2D matrix so that I can use it to calculate inverse.For that I am using numpy.matrix(array) but it is not working. Can anyone tell how to convert a 2D array to a numpy matrix? The array consists of all float numbers
2 Answers
If a is your array, np.asmatrix(a) is a matrix.
If you have a list of lists (as you mentioned), you need to convert it first to a numpy array; see how to convert 2d list to 2d numpy array?
A short example is given here:
import numpy as np
a = [[ 0. +0.j, 1.j, 2. -2.j],
[ 4. -4.j, 5. -5.j, 6. -1.j],
[ 8. -8.j, 9. -9.j, 10.]]
b = np.matrix(np.array(a))
b_inv = np.linalg.inv(b)