Python NumPy Matrices
godarda@gd:~$ python3
...
>>> from numpy import *
>>> arr = arange(1, 18, 2)
>>> arr
array([ 1, 3, 5, 7, 9, 11, 13, 15, 17])
>>> arr = arr.reshape(3, 3)
>>> arr
array([[ 1, 3, 5],
[ 7, 9, 11],
[13, 15, 17]])
>>> m = matrix(arr)
>>> m
matrix([[ 1, 3, 5],
[ 7, 9, 11],
[13, 15, 17]])
>>> diagonal(m)
array([ 1, 9, 17])
>>> m.max()
17
>>> m.min()
1
>>> m.sum()
81
>>> m.mean()
9.0
>>> m[0,0] = 99
>>> m
matrix([[99, 3, 5],
[ 7, 9, 11],
[13, 15, 17]])
>>> m = sort(m)
>>> m
matrix([[ 3, 5, 99],
[ 7, 9, 11],
[13, 15, 17]])
>>> m.transpose()
matrix([[ 3, 7, 13],
[ 5, 9, 15],
[99, 11, 17]])
Products of Elements
>>> m = matrix("1 2 3; 4 5 6; 7 8 9")
>>> m
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> m.prod(0) # Products column wise
matrix([[ 28, 80, 162]])
>>> m.prod(1) # Products row wise
matrix([[ 6],
[120],
[504]])
Comments and Reactions
What Next?
Python to print the transpose of a given matrix
Python to perform arithmetic operations on two matricesPython File HandlingAdvertisement