Creating a matrix
In SciPy, a matrix structure is given to any one- or two-dimensional ndarray
, with either the matrix
or mat
command. The complete syntax is as follows:
numpy.matrix(data=object, dtype=None, copy=True)
Creating matrices, the data may be given as ndarray
, a string or a Python list (as the second example below), which is very convenient. When using strings, the semicolon denotes change of row and the comma, change of column:
>>> A=numpy.matrix("1,2,3;4,5,6") >>> A
The output is shown a follows s:
matrix([[1, 2, 3], [4, 5, 6]])
Let's look at another example:
>>> A=numpy.matrix([[1,2,3],[4,5,6]]) >>> A
The output is shown as follows:
matrix([[1, 2, 3], [4, 5, 6]])
Another technique to create a matrix from a two-dimensional array is to enforce the matrix structure on a new object, copying the data of the former with the asmatrix
routine.
A matrix is said to be sparse (http://en.wikipedia.org/wiki/Sparse_matrix) if most...