Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning SciPy for Numerical and Scientific Computing Second Edition

You're reading from  Learning SciPy for Numerical and Scientific Computing Second Edition

Product type Book
Published in Feb 2015
Publisher Packt
ISBN-13 9781783987702
Pages 188 pages
Edition 1st Edition
Languages

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 of its entries...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}