Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent matrices in python

Tags:

python

matrix

How can I represent matrices in python?

like image 280
Bunny Rabbit Avatar asked Jun 27 '10 13:06

Bunny Rabbit


People also ask

How is a matrix represented in Python?

In Python, Matrix is represented by the list data type. We are going to teach you how to create a 3x3 matrix using the list. The matrix is ​​made up of three rows and three columns.

How do you represent a matrix in numpy?

The numpy ndarray class is used to represent both matrices and vectors. To construct a matrix in numpy we list the rows of the matrix in a list and pass that list to the numpy array constructor. The first slice selects all rows in A, while the second slice selects just the middle entry in each row.

How do you make a 3 by 3 matrix in Python?

You can use numpy. First, convert your list into numpy array. Then, take an element and reshape it to 3x3 matrix.


2 Answers

Take a look at this answer:

from numpy import matrix from numpy import linalg A = matrix( [[1,2,3],[11,12,13],[21,22,23]]) # Creates a matrix. x = matrix( [[1],[2],[3]] )                  # Creates a matrix (like a column vector). y = matrix( [[1,2,3]] )                      # Creates a matrix (like a row vector). print A.T                                    # Transpose of A. print A*x                                    # Matrix multiplication of A and x. print A.I                                    # Inverse of A. print linalg.solve(A, x)     # Solve the linear equation system. 
like image 169
György Andrasek Avatar answered Oct 01 '22 23:10

György Andrasek


Python doesn't have matrices. You can use a list of lists or NumPy

like image 36
Ed. Avatar answered Oct 01 '22 22:10

Ed.