## Solution Exercise 8: Duplicating a numpy matrix

Exercise 8 Generate a numpy matrix by repeating a smaller 2-dimensional, 5 times. Solution import numpy as npA = np.array([[5 , 7], [3 , 9]])C = np.tile(A , 5)print(C)"""attach:[[5 7 5 7 5 7 5 7 5 7] [3 9 3 9 3 9 3 9 3 9]] """ Younes Derfoufimy-courses.net

## Solution Exercise 7: Stack two numpy matrices vertically

Exercise 7 Stack 2 numpy arrays vertically, i.e. 2 arrays with the same last dimension (same number of columns). Solution import numpy as npA = np.array([[1 , 2], [3 , 4] ])B = np.array([[5 , 6], [7 , 8] ])C = np.vstack((A, B))print(C)"""attach:[[1 2] [3 4] [5 6] [7 8]] """ Younes Derfoufimy-courses.net

## Solution Exercise 6: Stack two numpy matrices horizontally

Exercise 6 Stack 2 numpy marices horizontally, i.e. 2 arrays having the same 1st dimension (same number of rows). Solution import numpy as npA = np.array([[1 , 2], [6 , 7]])B = np.array([[3 , 4 , 5], [8 , 9 , 10]])C = np.hstack((A, B))print(C)"""attach:[[ 1 2 3 4 5] [ 6 7 8 9…

## Solution Exercise 5: Convert a binary numpy matrix to a boolean

Exercise 5 Convert a numpy binary matrix (containing only '0' and '1') to a numpy boolean matrix (i.e. '1' will be replaced by 'True' and '0' by 'False') Solution import numpy as npA = np.array([[1, 0, 1], [0, 0, 1], [1, 1, 0]])B = A.astype('bool')print(B)"""output:[[ True False True] [False False True] [True True False]] """…

## Solution Exercise 4: trace of numpy matrix without using the trace() method

Exercise 4 Resume the previous exercise (Exercise3) without using the trace() method. Solution import numpy as npdef matrix_trace(A): n = len(A) # initialization of trace of A trace = 0 for i in range(0, n): #trace = trace + aii trace = trace + A[i][i] return trace#ExampleA = np.array([[1, 3, 4], [1, 3, 5], [3,…

## Solution Exercise 3: trace of numpy matrix

Exercise 3 Write a python function that takes a numpy matrix as a parameter and returns its trace. We recall that the trace of a square matrix A = (ai j)i,j is the number Tr(A) = a11 + a22 + ... + ann Solution import numpy as npdef matrix_trace(A): return A.trace()#ExampleA = np.array([[2,3],[1,5]])print("Tr(A) = ",…