PW Matrices and Vectors: The Index 0 (N, M) NXM N M (I) (J)
PW Matrices and Vectors: The Index 0 (N, M) NXM N M (I) (J)
6. Functions on matrices:
There are many predefined functions in Python's numpy package that are applied to matrices,
either to create matrix-types, or for common operations to be applied to the entire matrix, or to its
rows or columns separately.
Application.4 : Explore the predefined matrices and some common calculations.
>>> A = np.eye(4, 4) >>> np.concatenate((A, B), 0) >>> F = np.diag(E)
>>> B = np.ones((4,1)) >>>np. concatenate((A, B), 1)
>>> C = np.zeros((4, 4)) >>> E = np.diag(D)
>>> D = np.random.randint(100, size=(3, 3))
Application.5: Make some statistics about the matrix A defined in the application.4.
>>> A = np.arange(1, 10).reshape(3, 3) >>> A.max(),A.min() >>> np.sort(A) # A.sort()
>>> np.sum(A),np. sum(A, 0) >>> A.max(0) >>> np.sort(A, 0)
>>> np. sum(A, 1) >>> A.min(1) >>> A[A % 2 == 0]
>>> np.prod(A), np.prod(A, 0) >>> A[:1, :].max() >>> A[(A>4) & (A<7)]
>>> np.mean(A), np.mean(A, 1) >>> A[1:, 1:].min() >>> where(A > 8) & (A < 45) )
7. Training :
Exercise.1: Solving a linear system
We consider the system of 3 equations:
M
C
1 2 … 10
Exercise.5: Matrices & functions
11 12 … 20 1 10
a) Define the following functions: 91 100
1. MCoins(M) : flips the four corners of matrix M into a
matrix C of size (2,2). 91 92 … 100
0 4 5 0 0 5
0 5 19
4 5 19
V1=4, V2=0 7 2 0
7 2 4
- Modify the MReplace(M,v1,v2) function, so that it also returns the number of replacements n.
b)
- Create a matrix A (4x4) of positive integer random values <10.
- Construct the matrix B(2x2) which contains the four corners of A.
- Replace a value in the matrix A with a zero (0).
8.2 Exercise.
Given the list of students' grades:
Notes = [12 , 04 , 14 , 11 , 18 , 13 , 07, 10 , 05 , 09 , 15 , 08 , 14 , 16]
Write a Python script that allows:
- Create a list of admitted students containing only grades above the average (grades >= 10).
- Calculate and display the number of even elements in the grade list.