An Introduction to Python For Undergraduate Engineers/Matrices

From Wikibooks, open books for an open world
Jump to: navigation, search

Matrices can be created in the following way:

   mymatrix = Matrix([[1,2],[3,4]])

In this example, [1,2] produces the first line of the matrix and [2,3] creates the second.

Quick Standard Matrices[edit]

The following built in functions can be used to quickly create some standard matrices of specified size.

Command Description Example
eye() Identity Matrix eye(4) creates a 4x4 identity matrix
zeros() Empty Matrix zeros(2,3) creates a 2x3 matrix of zeros
ones() Matrix of 1s ones(2) creates a 2x2 matrix of ones

Getting the Size of a Matrix[edit]

You can find out the size of a matrix using the .shape[] command, for example:

   mymatrix.shape[0]
Command Description
.shape[0] Gives number of rows
.shape[1] Gives number of columns