Hướng dẫn matrix subtraction in python - phép trừ ma trận trong python

View Discussion

Nội dung chính

  • Sử dụng ma trận trong Python với Numpy
  • Một số thao tác phép toán trong Numpy
  • Ví dụ về ma trận trong Python
  • Một số thao tác tạo ma trận trong Python
  • Thêm một cột
  • Xóa một hàng tạo thành một Ma trận
  • Xóa một cột khỏi Ma trận
  • Cập nhật một hàng trong Ma trận

Nội dung chính

  • Sử dụng ma trận trong Python với Numpy
  • Một số thao tác phép toán trong Numpy
  • Ví dụ về ma trận trong Python
  • Một số thao tác tạo ma trận trong Python
  • Thêm một cột
  • Xóa một hàng tạo thành một Ma trận
  • Xóa một cột khỏi Ma trận
  • Cập nhật một hàng trong Ma trận

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Read function is used when we want to compute the difference of two array.It returns the difference of arr1 and arr2, element-wise.

    Discuss numpy.subtract(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘subtract’)

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    6 function is used when we want to compute the difference of two array.It returns the difference of arr1 and arr2, element-wise.
    arr1 : [array_like or scalar]1st Input array.
    arr2 : [array_like or scalar]2nd Input array.
    dtype : The type of the returned array. By default, the dtype of arr is used.
    out : [ndarray, optional] A location into which the result is stored.
      -> If provided, it must have a shape that the inputs broadcast to.
      -> If not provided or None, a freshly-allocated array is returned.
    where : [array_like, optional] Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.
    **kwargs : Allows to pass keyword variable length of argument to a function. Used when we want to handle named argument in a function.

    Syntax : numpy.subtract(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘subtract’) [ndarray or scalar] The difference of arr1 and arr2, element-wise. Returns a scalar if both arr1 and arr2 are scalars.

    Parameters :arr1 : [array_like or scalar]1st Input array.arr2 : [array_like or scalar]2nd Input array.dtype : The type of the returned array. By default, the dtype of arr is used.out : [ndarray, optional] A location into which the result is stored.  -> If provided, it must have a shape that the inputs broadcast to.  -> If not provided or None, a freshly-allocated array is returned.where : [array_like, optional] Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.**kwargs : Allows to pass keyword variable length of argument to a function. Used when we want to handle named argument in a function.

    Return : [ndarray or scalar] The difference of arr1 and arr2, element-wise. Returns a scalar if both arr1 and arr2 are scalars.

    Code #1 :

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    7
    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    8

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    9
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    0
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    1

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    2
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    0
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    4

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    5
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    6
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    7
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    8

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    5
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    6
    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    1
    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    2

    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    3
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    0
    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    5

    1st Input number :  4
    2nd Input number :  6
    Difference of two input number :  -2
    

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    5
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    6
    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    8
    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    9

    Return : [ndarray or scalar] The difference of arr1 and arr2, element-wise. Returns a scalar if both arr1 and arr2 are scalars.

    Code #1 :

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    7
    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    8

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    9
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    0
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    1

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    2
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    0
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    4

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    5
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    6
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    7
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    8

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    5
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    6
    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    1
    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    2

    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    3
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    0
    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    5

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    

    • # Python code to demonstrate matrix operations
      
      # add(), subtract() and divide()
      
      # importing numpy for matrix operations
      
      import numpy
      
      # initializing matrices
      
      x = numpy.array([[1, 2], [4, 5]])
      
      y = numpy.array([[7, 8], [9, 10]])
      
      # using add() to add matrices
      
      print ("The element wise addition of matrix is : ")
      
      print (numpy.add(x,y))
      
        
      
      # using subtract() to subtract matrices
      
      print ("The element wise subtraction of matrix is : ")
      
      print (numpy.subtract(x,y))
      
        
      
      # using divide() to divide matrices
      
      print ("The element wise division of matrix is : ")
      
      print (numpy.divide(x,y))
      5
      # Python code to demonstrate matrix operations
      
      # add(), subtract() and divide()
      
      # importing numpy for matrix operations
      
      import numpy
      
      # initializing matrices
      
      x = numpy.array([[1, 2], [4, 5]])
      
      y = numpy.array([[7, 8], [9, 10]])
      
      # using add() to add matrices
      
      print ("The element wise addition of matrix is : ")
      
      print (numpy.add(x,y))
      
        
      
      # using subtract() to subtract matrices
      
      print ("The element wise subtraction of matrix is : ")
      
      print (numpy.subtract(x,y))
      
        
      
      # using divide() to divide matrices
      
      print ("The element wise division of matrix is : ")
      
      print (numpy.divide(x,y))
      6
      The element wise addition of matrix is : 
      
      [[ 8 10]
      
       [13 15]]
      
      The element wise subtraction of matrix is : 
      
      [[-6 -6]
      
       [-5 -5]]
      
      The element wise division of matrix is : 
      
      [[ 0.14285714  0.25      ]
      
       [ 0.44444444  0.5       ]]
      8
      The element wise addition of matrix is : 
      
      [[ 8 10]
      
       [13 15]]
      
      The element wise subtraction of matrix is : 
      
      [[-6 -6]
      
       [-5 -5]]
      
      The element wise division of matrix is : 
      
      [[ 0.14285714  0.25      ]
      
       [ 0.44444444  0.5       ]]
      9
    • Output :

    Code #2 :

    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    2
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    0
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    4
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    5
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    6
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    7
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    1
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    6
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    0
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    1
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    7
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    4
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    6
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    5
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    6
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    7
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    8

    Sử dụng ma trận trong Python với Numpy

    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    9
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    0
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    4
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    7
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    6
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    7
    ['Wed', 15, 21, 20, 19]
    
    23
    5
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    6
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    0
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    1
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    0
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    6
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    7
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    5
    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)
    6
    from numpy import * 
    
    m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m_r = append(m,[['Avg',12,15,13,11]],0)
    
    
    
    
    print(m_r)
    
    Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau:
    
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']
    
     ['Avg' '12' '15' '13' '11']]
    4
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]
    8NumPy là thư viện được viết bằng Python nhằm phục vụ cho việc tính toán khoa học, hỗ trợ nhiều kiểu dữ liệu đa chiều giúp cho việc tính toán, lập trình, làm việc với các hệ cơ sở dữ liệu cực kì thuận tiện. Bạn có thể sử dụng các các phép toán và mảng (array) được định nghĩa trong numpy để tạo ma trận. Array này là một đối tượng mảng đa chiều thuần nhất tức là mọi phần tử đều cùng 1 kiểu.

    Một số thao tác phép toán trong Numpy

    1. # Python code to demonstrate matrix operations
      
      # add(), subtract() and divide()
      
      # importing numpy for matrix operations
      
      import numpy
      
      # initializing matrices
      
      x = numpy.array([[1, 2], [4, 5]])
      
      y = numpy.array([[7, 8], [9, 10]])
      
      # using add() to add matrices
      
      print ("The element wise addition of matrix is : ")
      
      print (numpy.add(x,y))
      
        
      
      # using subtract() to subtract matrices
      
      print ("The element wise subtraction of matrix is : ")
      
      print (numpy.subtract(x,y))
      
        
      
      # using divide() to divide matrices
      
      print ("The element wise division of matrix is : ")
      
      print (numpy.divide(x,y))
      5
      # Python code to demonstrate matrix operations
      
      # add(), subtract() and divide()
      
      # importing numpy for matrix operations
      
      import numpy
      
      # initializing matrices
      
      x = numpy.array([[1, 2], [4, 5]])
      
      y = numpy.array([[7, 8], [9, 10]])
      
      # using add() to add matrices
      
      print ("The element wise addition of matrix is : ")
      
      print (numpy.add(x,y))
      
        
      
      # using subtract() to subtract matrices
      
      print ("The element wise subtraction of matrix is : ")
      
      print (numpy.subtract(x,y))
      
        
      
      # using divide() to divide matrices
      
      print ("The element wise division of matrix is : ")
      
      print (numpy.divide(x,y))
      6
      from numpy import * 
      
      m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
      
        ['Wed',15,21,20,19],['Thu',11,20,22,21],
      
        ['Fri',18,17,23,22],['Sat',12,22,20,18],
      
        ['Sun',13,15,19,16]])
      
          
      
      m_r = append(m,[['Avg',12,15,13,11]],0)
      
      
      
      
      print(m_r)
      
      Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau:
      
      [['Mon' '18' '20' '22' '17']
      
       ['Tue' '11' '18' '21' '18']
      
       ['Wed' '15' '21' '20' '19']
      
       ['Thu' '11' '20' '22' '21']
      
       ['Fri' '18' '17' '23' '22']
      
       ['Sat' '12' '22' '20' '18']
      
       ['Sun' '13' '15' '19' '16']
      
       ['Avg' '12' '15' '13' '11']]
      8
      from numpy import * 
      
      m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
      
        ['Wed',15,21,20,19],['Thu',11,20,22,21],
      
        ['Fri',18,17,23,22],['Sat',12,22,20,18],
      
        ['Sun',13,15,19,16]])
      
          
      
      m_r = append(m,[['Avg',12,15,13,11]],0)
      
      
      
      
      print(m_r)
      
      Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau:
      
      [['Mon' '18' '20' '22' '17']
      
       ['Tue' '11' '18' '21' '18']
      
       ['Wed' '15' '21' '20' '19']
      
       ['Thu' '11' '20' '22' '21']
      
       ['Fri' '18' '17' '23' '22']
      
       ['Sat' '12' '22' '20' '18']
      
       ['Sun' '13' '15' '19' '16']
      
       ['Avg' '12' '15' '13' '11']]
      9
      Hàm này được sử dụng để thực hiện phép cộng ma trận phần tử wise 
    2. # Python code to demonstrate matrix operations
      
      # add(), subtract() and divide()
      
      # importing numpy for matrix operations
      
      import numpy
      
      # initializing matrices
      
      x = numpy.array([[1, 2], [4, 5]])
      
      y = numpy.array([[7, 8], [9, 10]])
      
      # using add() to add matrices
      
      print ("The element wise addition of matrix is : ")
      
      print (numpy.add(x,y))
      
        
      
      # using subtract() to subtract matrices
      
      print ("The element wise subtraction of matrix is : ")
      
      print (numpy.subtract(x,y))
      
        
      
      # using divide() to divide matrices
      
      print ("The element wise division of matrix is : ")
      
      print (numpy.divide(x,y))
      5
      # Python code to demonstrate matrix operations
      
      # add(), subtract() and divide()
      
      # importing numpy for matrix operations
      
      import numpy
      
      # initializing matrices
      
      x = numpy.array([[1, 2], [4, 5]])
      
      y = numpy.array([[7, 8], [9, 10]])
      
      # using add() to add matrices
      
      print ("The element wise addition of matrix is : ")
      
      print (numpy.add(x,y))
      
        
      
      # using subtract() to subtract matrices
      
      print ("The element wise subtraction of matrix is : ")
      
      print (numpy.subtract(x,y))
      
        
      
      # using divide() to divide matrices
      
      print ("The element wise division of matrix is : ")
      
      print (numpy.divide(x,y))
      6
      from numpy import * 
      
      m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
      
        ['Wed',15,21,20,19],['Thu',11,20,22,21],
      
        ['Fri',18,17,23,22],['Sat',12,22,20,18],
      
        ['Sun',13,15,19,16]])
      
          
      
      m_c = insert(m,[5],[[1],[2],[3],[4],[5],[6],[7]],1)
      
      
      
      
      print(m_c)
      2
      from numpy import * 
      
      m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
      
        ['Wed',15,21,20,19],['Thu',11,20,22,21],
      
        ['Fri',18,17,23,22],['Sat',12,22,20,18],
      
        ['Sun',13,15,19,16]])
      
          
      
      m_c = insert(m,[5],[[1],[2],[3],[4],[5],[6],[7]],1)
      
      
      
      
      print(m_c)
      3
      Hàm này dùng để thực hiện phép trừ ma trận phần tử .
    3. from numpy import * 
      
      m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
      
        ['Wed',15,21,20,19],['Thu',11,20,22,21],
      
        ['Fri',18,17,23,22],['Sat',12,22,20,18],
      
        ['Sun',13,15,19,16]])
      
          
      
      m_c = insert(m,[5],[[1],[2],[3],[4],[5],[6],[7]],1)
      
      
      
      
      print(m_c)
      4
      # Python code to demonstrate matrix operations
      
      # add(), subtract() and divide()
      
      # importing numpy for matrix operations
      
      import numpy
      
      # initializing matrices
      
      x = numpy.array([[1, 2], [4, 5]])
      
      y = numpy.array([[7, 8], [9, 10]])
      
      # using add() to add matrices
      
      print ("The element wise addition of matrix is : ")
      
      print (numpy.add(x,y))
      
        
      
      # using subtract() to subtract matrices
      
      print ("The element wise subtraction of matrix is : ")
      
      print (numpy.subtract(x,y))
      
        
      
      # using divide() to divide matrices
      
      print ("The element wise division of matrix is : ")
      
      print (numpy.divide(x,y))
      0
      from numpy import * 
      
      m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
      
        ['Wed',15,21,20,19],['Thu',11,20,22,21],
      
        ['Fri',18,17,23,22],['Sat',12,22,20,18],
      
        ['Sun',13,15,19,16]])
      
          
      
      m_c = insert(m,[5],[[1],[2],[3],[4],[5],[6],[7]],1)
      
      
      
      
      print(m_c)
      6
      Hàm này được sử dụng để thực hiện phép chia ma trận phần tử wise.

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    5
    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))
    6
    from numpy import * 
    
    m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m_c = insert(m,[5],[[1],[2],[3],[4],[5],[6],[7]],1)
    
    
    
    
    print(m_c)
    9
    [['Mon' '18' '20' '22' '17' '1']
    
     ['Tue' '11' '18' '21' '18' '2']
    
     ['Wed' '15' '21' '20' '19' '3']
    
     ['Thu' '11' '20' '22' '21' '4']
    
     ['Fri' '18' '17' '23' '22' '5']
    
     ['Sat' '12' '22' '20' '18' '6']
    
     ['Sun' '13' '15' '19' '16' '7']]
    0

    # Python code to demonstrate matrix operations
    
    # add(), subtract() and divide()
    
    # importing numpy for matrix operations
    
    import numpy
    
    # initializing matrices
    
    x = numpy.array([[1, 2], [4, 5]])
    
    y = numpy.array([[7, 8], [9, 10]])
    
    # using add() to add matrices
    
    print ("The element wise addition of matrix is : ")
    
    print (numpy.add(x,y))
    
      
    
    # using subtract() to subtract matrices
    
    print ("The element wise subtraction of matrix is : ")
    
    print (numpy.subtract(x,y))
    
      
    
    # using divide() to divide matrices
    
    print ("The element wise division of matrix is : ")
    
    print (numpy.divide(x,y))

    Blog

    The element wise addition of matrix is : 
    
    [[ 8 10]
    
     [13 15]]
    
    The element wise subtraction of matrix is : 
    
    [[-6 -6]
    
     [-5 -5]]
    
    The element wise division of matrix is : 
    
    [[ 0.14285714  0.25      ]
    
     [ 0.44444444  0.5       ]]
    1. Tin tức Hàm này được sử dụng để thực hiện phép nhân ma trận phần tử .
    2. 21/09/2021 01:01 Hàm này được sử dụng để tính toán phép nhân ma trận, chứ không phải phép nhân phần tử wise .
    3. Ma trận Python là một trường hợp đặc biệt của cấu trúc dữ liệu hai chiều, trong đó mỗi phần tử dữ liệu có cùng kích thước, và các số được sắp xếp thành các hàng và cột. Mọi ma trận đều là một mảng hai chiều nhưng không ngược lại. Ma trận là cấu trúc dữ liệu quan trọng cho phép tính toán và khoa học. Trong bài viết này, hãy cùng T3H tìm hiểu thêm thông tin về ma trận trong Python cũng như một số thông tin về ma trận này! Hàm này dùng để tính căn bậc hai của từng phần tử của ma trận.
    4. Để thực hiện các thao tác dữ liệu ma trận, bạn có thể sử dụng gói numpy. NumPy là thư viện được viết bằng Python nhằm phục vụ cho việc tính toán khoa học, hỗ trợ nhiều kiểu dữ liệu đa chiều giúp cho việc tính toán, lập trình, làm việc với các hệ cơ sở dữ liệu cực kì thuận tiện. Bạn có thể sử dụng các các phép toán và mảng (array) được định nghĩa trong numpy để tạo ma trận. Array này là một đối tượng mảng đa chiều thuần nhất tức là mọi phần tử đều cùng 1 kiểu. Hàm này dùng để cộng tất cả các phần tử trong ma trận . Đối số "trục" tùy chọn tính tổng cột nếu trục là 0tổng hàng nếu trục là 1 .
    5. add (): - Hàm này được sử dụng để thực hiện phép cộng ma trận phần tử wise  Đối số này được sử dụng để chuyển vị trí của ma trận được chỉ định.

    Ví dụ về ma trận trong Python

    subtract (): - Hàm này dùng để thực hiện phép trừ ma trận phần tử .

    from numpy import * 
    
    a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m = reshape(a,(7,5))
    
    print(m)

    Dữ liệu trên có thể được biểu diễn dưới dạng một mảng hai chiều như bên dưới.

    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']]

    Khi đoạn mã trên được thực thi, nó sẽ cho ra kết quả như sau:

    ['Wed', 15, 21, 20, 19]
    
    23

    Một số thao tác tạo ma trận trong Python

    from numpy import * 
    
    m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m_r = append(m,[['Avg',12,15,13,11]],0)
    
    
    
    
    print(m_r)
    
    Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau:
    
    [['Mon' '18' '20' '22' '17']
    
     ['Tue' '11' '18' '21' '18']
    
     ['Wed' '15' '21' '20' '19']
    
     ['Thu' '11' '20' '22' '21']
    
     ['Fri' '18' '17' '23' '22']
    
     ['Sat' '12' '22' '20' '18']
    
     ['Sun' '13' '15' '19' '16']
    
     ['Avg' '12' '15' '13' '11']]

    >>> Đọc thêm: Biến trong Python - Bạn đã biết gì về biến trong PythonBiến trong Python - Bạn đã biết gì về biến trong Python

    Thêm một cột

    Chúng ta có thể thêm cột vào ma trận bằng phương thức insert (). ở đây chúng ta phải đề cập đến chỉ mục nơi chúng ta muốn thêm cột và một mảng chứa các giá trị mới của các cột được thêm vào. Trong ví dụ dưới đây, một cột mới đã được thêm từ vị trí thứ 5

    from numpy import * 
    
    m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
    
      ['Wed',15,21,20,19],['Thu',11,20,22,21],
    
      ['Fri',18,17,23,22],['Sat',12,22,20,18],
    
      ['Sun',13,15,19,16]])
    
        
    
    m_c = insert(m,[5],[[1],[2],[3],[4],[5],[6],[7]],1)
    
    
    
    
    print(m_c)

    Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau:

    [['Mon' '18' '20' '22' '17' '1']
    
     ['Tue' '11' '18' '21' '18' '2']
    
     ['Wed' '15' '21' '20' '19' '3']
    
     ['Thu' '11' '20' '22' '21' '4']
    
     ['Fri' '18' '17' '23' '22' '5']
    
     ['Sat' '12' '22' '20' '18' '6']
    
     ['Sun' '13' '15' '19' '16' '7']]

    Xóa một hàng tạo thành một Ma trận

    Chúng ta có thể xóa một hàng khỏi ma trận bằng phương thức delete (). Chúng ta phải chỉ định chỉ số của hàng và giá trị trục là 0 cho một hàng và 1 cho một cột.

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    0

    Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau:

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    1

    Xóa một hàng tạo thành một Ma trận

    Chúng ta có thể xóa một hàng khỏi ma trận bằng phương thức delete (). Chúng ta phải chỉ định chỉ số của hàng và giá trị trục là 0 cho một hàng và 1 cho một cột.

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    2

    Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau:

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    3

    Xóa một hàng tạo thành một Ma trận

    Chúng ta có thể xóa một hàng khỏi ma trận bằng phương thức delete (). Chúng ta phải chỉ định chỉ số của hàng và giá trị trục là 0 cho một hàng và 1 cho một cột.

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    4

    Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau:

    1st Input array :  [[ 2 -4  5]
     [-6  2  0]]
    2nd Input array :  [[ 0 -7  5]
     [ 5 -2  9]]
    Output array:  [[  2   3   0]
     [-11   4  -9]]
    
    
    5

    Xóa một hàng tạo thành một Ma trận Trên đây là các thông tin quan trọng về ma trận trong Python. Bạn có thể tìm hiểu thêm thông tin về Python tại mục blog của Viện công nghệ thông tin T3H.