Chúng ta có thể dễ dàng thực hiện thao tác ma trận trong Python bằng thư viện Numpy. NumPy là một gói Python. Nó là viết tắt của 'Python số'. Nó là một thư viện bao gồm các đối tượng mảng đa chiều và một tập hợp các thủ tục để xử lý mảng. Sử dụng NumPy, các phép toán và logic trên mảng có thể được thực hiện
Cài đặt và nhập Numpy
Để cài đặt Numpy, hãy sử dụng pip -
pip install numpy
Nhập Numpy −
import numpy
Cộng, trừ, chia và nhân ma trận
Chúng tôi sẽ sử dụng các phương thức Numpy sau đây để thao tác với ma trận
cục mịch. add[] - Cộng hai ma trận
cục mịch. trừ[] - Trừ hai ma trận
cục mịch. chia [] - Chia hai ma trận
cục mịch. multi[] - Nhân hai ma trận
Bây giờ chúng ta hãy xem mã
Ví dụ
đầu ra
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]
Tổng các phần tử ma trận
Phương thức sum[] được sử dụng để tìm tổng -
Ví dụ
đầu ra
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]
Chuyển đổi từ ma trận
Các. Thuộc tính T được sử dụng để tìm Chuyển vị của Ma trận −
Ví dụ
đầu ra
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]
Python được biết đến với tính năng xử lý và đọc dữ liệu rõ ràng và gọn gàng. Có nhiều kỹ thuật khác nhau để xử lý dữ liệu trong Python như sử dụng Từ điển, Bộ dữ liệu, Ma trận, v.v. Trong hướng dẫn này, bạn sẽ tìm hiểu về ma trận và các chức năng của nó
Ma trận trong Python là gì?
Đây là những cấu trúc dữ liệu 2D [hai chiều]. Trong các dự án trực tiếp và mô phỏng dữ liệu thực, bạn phải giữ dữ liệu ở định dạng tuần tự hoặc dạng bảng. Giả sử bạn muốn lưu trữ dữ liệu của ba nhân viên thuộc ba phòng ban khác nhau. Vì vậy, ở định dạng của các bảng, nó sẽ trông giống như
ID nhân viênTên nhân viênPhòng nhân viên1001ARayTrưởng phòng kỹ thuật2004BKarlosManager3100AAlexLead DeveloperTrong Python, các bảng này được gọi là mảng hai chiều, còn được gọi là ma trận. Python cung cấp cho chúng tôi cơ sở để biểu diễn những dữ liệu này dưới dạng danh sách
Ví dụ
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
Trong đoạn mã trên, val đại diện cho ma trận 3 X 3 trong đó có ba hàng và ba cột
Tạo một ma trận trong Python
Python cho phép các nhà phát triển triển khai ma trận bằng cách sử dụng danh sách lồng nhau. Danh sách có thể được tạo nếu bạn đặt tất cả các mục hoặc thành phần bắt đầu bằng '[' và kết thúc bằng ']' [dấu ngoặc vuông] và phân tách từng thành phần bằng dấu phẩy
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
Tự động tạo ma trận trong Python
Có thể tạo ma trận n x m bằng cách liệt kê một tập hợp các phần tử [giả sử n] và sau đó làm cho mỗi phần tử được liên kết với một danh sách 1D khác gồm m phần tử. Đây là một đoạn mã cho điều này
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
Đầu ra của chương trình sẽ là
________số 8Truy cập các phần tử của ma trận
Có nhiều phương pháp khác nhau để lấy các phần tử của ma trận trong Python
i] Chỉ mục. Để truy cập các phần tử của ma trận, bạn phải sử dụng dấu ngoặc vuông sau tên biến với số hàng và cột, như thế này val[row_no][col_no]
val = [['Dave',101,90,95], ['Alex',102,85,100], ['Ray',103,90,95]]
print[val[2]]
print[val[0][1]]
đầu ra
import numpy0
ii] Lập chỉ mục tiêu cực. Python cũng cung cấp tính năng lập chỉ mục với các giá trị âm trong dấu ngoặc vuông của nó để truy cập các giá trị một cách tuần tự. Trong lập chỉ mục phủ định, -1 đề cập đến phần tử cuối cùng của ma trận, -2 là phần tử cuối cùng thứ 2 và tiếp tục
Ở đây chúng ta sẽ thảo luận về các cách khác nhau để tạo ma trận bằng Python trong hướng dẫn này, chúng ta cũng sẽ thảo luận về các thao tác khác nhau có thể được thực hiện trên ma trận. chúng tôi cũng sẽ đề cập đến mô-đun bên ngoài Numpy để tạo ma trận và các hoạt động của nó trong Python
Ma trận là gì?
Ma trận là tập hợp các số được sắp xếp trong một mảng hình chữ nhật theo hàng và cột. Trong các lĩnh vực kỹ thuật, vật lý, thống kê và đồ họa, ma trận được sử dụng rộng rãi để thể hiện phép quay ảnh và các loại phép biến đổi khác.
Ma trận được gọi là ma trận m nhân n, được ký hiệu bằng ký hiệu "m x n" nếu có m hàng, n cột.
Tạo một ma trận đơn giản bằng Python
Phương pháp 1. Tạo ma trận với Danh sách danh sách
Ở đây, chúng ta sẽ tạo một ma trận bằng cách sử dụng danh sách các danh sách
Python3
import numpy19
import numpy20
import numpy21_______122
import numpy23
import numpy24
import numpy23
import numpy26
import numpy23
import numpy28
import numpy29
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]21____222
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]29
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]21____342
import numpy23
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]44
import numpy23______346
import numpy23
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]48
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
_______421____422____423
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]24
đầu ra
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]7
Phương pháp 2. Lấy đầu vào Ma trận từ người dùng trong Python
Ở đây, chúng tôi đang lấy một số hàng và cột từ người dùng và in Ma trận
Python3
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]25
import numpy20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]27
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]29
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22____541
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
42val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
43import numpy20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]27
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]29
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
49val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
42Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
52import numpy19
import numpy20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
55_______421____422____658
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
59Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
41n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
42n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
47Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
49import numpy20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
55Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]63
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46[[0, 0, 0], [0, 0, 0], [0, 0, 0]]69
import numpy100
import numpy101
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]27______422
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]29
import numpy105
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
import numpy107
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
import numpy109
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46import numpy114
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46import numpy120
import numpy100
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
import numpy123
import numpy20
import numpy125
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
59_______220____421____1129
đầu ra
import numpy1
Phương pháp 3. Tạo một ma trận bằng cách sử dụng hiểu danh sách
Hiểu danh sách là một cách hay để xác định và tạo danh sách trong Python, chúng tôi đang sử dụng hàm phạm vi để in 4 hàng và 4 cột
Python3
import numpy19
import numpy20
import numpy132_______743
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
import numpy28
import numpy139
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
import numpy28
import numpy139
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
_______421____1149
đầu ra
import numpy2
Gán giá trị trong ma trận
Phương pháp 1. Gán một giá trị cho một ô riêng lẻ trong Ma trận
Ở đây chúng tôi đang thay thế và gán giá trị cho một ô riêng lẻ [1 hàng và 1 cột = 11] trong Ma trận
Python3
import numpy150
import numpy20
import numpy21
import numpy22
import numpy23
import numpy24_______123
import numpy26
import numpy158
import numpy28
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy158
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28
import numpy23
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]42
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44import numpy20
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
import numpy20
import numpy22
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
import numpy178
import numpy20
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]46
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
_______421____1183
đầu ra
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]2
Phương pháp 2. Gán một giá trị cho một ô riêng lẻ bằng cách sử dụng lập chỉ mục phủ định trong Ma trận
Ở đây chúng tôi đang thay thế và gán giá trị cho một ô riêng lẻ [-2 hàng và -1 cột = 21] trong Ma trận
Python3
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44import numpy20
import numpy186
import numpy24
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
import numpy20
import numpy186
import numpy22
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
import numpy178
import numpy20
import numpy195
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
_______421____1183
đầu ra
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]4
Truy cập giá trị trong một ma trận
Phương pháp 1. Truy cập các giá trị Ma trận
Ở đây, chúng tôi đang truy cập các phần tử của Ma trận bằng cách chuyển hàng và cột của nó
Python3
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
import numpy201
import numpy202
import numpy203
import numpy204
import numpy24
import numpy206
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
import numpy209
import numpy202
import numpy24
import numpy204
import numpy24
import numpy214
đầu ra
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]2
Phương pháp 2. Truy cập các giá trị Ma trận bằng cách sử dụng chỉ mục tiêu cực
Ở đây, chúng tôi đang truy cập các phần tử của Ma trận bằng cách chuyển hàng và cột của nó vào chỉ mục phủ định
Python3
import numpy215
import numpy216
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
import numpy150
import numpy20
import numpy21
import numpy22
import numpy23
import numpy24_______123
import numpy26
import numpy158
import numpy28
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy158
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28
import numpy23
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]42
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
import numpy241
import numpy186
import numpy22
import numpy204
import numpy186
import numpy24
import numpy214
đầu ra
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
4Các phép toán với ma trận trong Python
ví dụ 1. Thêm giá trị vào ma trận với vòng lặp for trong python
Ở đây, chúng tôi đang thêm hai ma trận bằng cách sử dụng vòng lặp for của Python
Python3
import numpy248
import numpy150
import numpy20
import numpy21
import numpy22
import numpy23
import numpy24_______123
import numpy26
import numpy257
import numpy28
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy158
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28
import numpy23
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]42
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
import numpy270
import numpy20
import numpy21
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]42
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28_______123
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy158
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
import numpy23
import numpy28
import numpy158
import numpy26
import numpy23
import numpy24
import numpy23
import numpy22
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
import numpy292
import numpy20
import numpy21
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
import numpy158
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
import numpy158
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]214
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]221
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]224
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
import numpy241
import numpy203
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]234
import numpy100
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]236
import numpy20
import numpy178
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]239
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]240
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]243____745
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]245
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20____421____2248
đầu ra
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
5ví dụ 2. Cộng và trừ các giá trị vào ma trận với khả năng hiểu danh sách
Thực hiện phép cộng và phép trừ cơ bản bằng cách sử dụng khả năng hiểu danh sách
Python3
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]249
import numpy20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]251____2239
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]240
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]254
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22____2220
import numpy241
import numpy203
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]263
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]254
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]271
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]272
import numpy20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]251____1186
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]240
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]254
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22____2220
import numpy241
import numpy203
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]263
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]254
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]271
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
_______421____422____2298
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
59n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]243____745
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]403
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20____421____2248
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
_______421____422____3410
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
59n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]243____745
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]415
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20____421____2248
đầu ra
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
4ví dụ 3. Chương trình Python để nhân và chia hai ma trận
Thực hiện phép nhân và chia cơ bản bằng vòng lặp Python
Python3
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]419
import numpy20
import numpy21
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
import numpy158
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
import numpy158
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]221
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
import numpy241
import numpy203
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]234
import numpy100
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]459
import numpy20
import numpy178
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]462
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]240
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]464
_______421____422____3467
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]468
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]243____745
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]472
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20____421____2248
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]464
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]478
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220____2221
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]486
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
import numpy241
import numpy203
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]234
import numpy100
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]459
import numpy20
import numpy178
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]498
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]498
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]240
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
_______421____422____4204
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]205
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]243____745
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]472
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20____421____2248
đầu ra
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]6
Hoán vị trong ma trận
Ví dụ. Chương trình Python để chuyển vị ma trận bằng vòng lặp
Chuyển vị của ma trận có được bằng cách thay đổi hàng thành cột và cột thành hàng. Nói cách khác, chuyển vị của A[][] có được bằng cách thay đổi A[i][j] thành A[j][i]
Python3
import numpy150
import numpy20
import numpy21
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]42
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy158
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
import numpy23
import numpy28
import numpy158
import numpy26
import numpy23
import numpy24
import numpy23
import numpy22
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
import numpy292
import numpy20
import numpy21
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
import numpy158
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
import numpy158
import numpy203
import numpy23
import numpy203
import numpy23
import numpy203
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]214
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
44n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]221
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]224
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]66
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
45 n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
46Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]220
import numpy241
import numpy203
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]234
import numpy100
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]278
import numpy20
import numpy178
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
43 Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]243____745
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]245
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20____421____2248
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]289
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]290
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]292
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]293
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]295
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]296
đầu ra
import numpy10
Ma trận sử dụng Numpy
Tạo ma trận bằng Numpy
Ở đây chúng tôi đang tạo một mảng Numpy bằng cách sử dụng numpy. ngẫu nhiên và một mô-đun ngẫu nhiên
Python3
import numpy215
import numpy216
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]299
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
400val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
401val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
402import numpy20
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
404Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]44
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
406import numpy20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
import numpy26
import numpy23
import numpy26
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
42_______421____5414
đầu ra
import numpy11
Các phép toán ma trận trong Python Sử dụng Numpy
Ở đây chúng tôi đang đề cập đến các phép toán khác nhau như cộng, trừ, nhân và chia bằng Numpy
Python3
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
415val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
416import numpy20
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
418import numpy22
import numpy23
import numpy24_______1158
import numpy28
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
426val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
427import numpy20
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
418Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28______1158
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]42
import numpy23
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]44
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
426Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
439Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22____5442
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
59Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
445Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
447Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22____5450
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
59Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
453Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22____5457____659
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
460Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
462Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22____5465
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
59Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
468đầu ra
import numpy12
Chấm và chéo sản phẩm với Matrix
Ở đây, chúng ta sẽ tìm các tích bên trong, bên ngoài và chéo của ma trận và vectơ bằng cách sử dụng NumPy trong Python
Python3
import numpy150
import numpy20
import numpy21
import numpy22
import numpy23
import numpy24_______123
import numpy26
import numpy257
import numpy28
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy257
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28
import numpy23
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]42
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
import numpy270
import numpy20
import numpy21
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]42
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28_______123
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]26
import numpy158
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
import numpy23
import numpy28
import numpy257
import numpy26
import numpy23
import numpy24
import numpy23
import numpy22
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
513import numpy20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
515_______421____422____6518
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
519Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
513import numpy20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
523_______421____422____6526
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
519đầu ra
import numpy13
Chuyển đổi ma trận trong Python bằng Numpy
Để thực hiện thao tác chuyển vị trên ma trận, chúng ta có thể sử dụng hàm numpy. phương thức chuyển vị[]
Python3
import numpy19
import numpy20
import numpy21_______122
import numpy23
import numpy24
import numpy23
import numpy26
import numpy158
import numpy28
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]22
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]49
_______421____422____6545
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
546đầu ra
import numpy14
Tạo một ma trận trống với NumPy trong Python
Khởi tạo một mảng trống, sử dụng lệnh np. số không[]
Python3
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
49import numpy20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
549import numpy24
import numpy23
import numpy24
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
553import numpy20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]27
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
59_______421____422____6559
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
560Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
562import numpy20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
549import numpy26
import numpy23
import numpy26
import numpy214
_______421____422____6571
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
572đầu ra
import numpy15
Cắt ma trận bằng Numpy
Cắt lát là quá trình chọn các hàng và cột cụ thể từ một ma trận, sau đó tạo một ma trận mới bằng cách loại bỏ tất cả các phần tử không được chọn. Trong ví dụ đầu tiên, chúng tôi đang in toàn bộ ma trận, trong lần thứ hai, chúng tôi chuyển 2 làm chỉ mục ban đầu, 3 làm chỉ mục cuối cùng và chỉ mục nhảy thành 1. Điều tương tự cũng được sử dụng trong bản in tiếp theo, chúng tôi vừa thay đổi bước nhảy chỉ mục thành 2
Python3
import numpy150
import numpy20
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
575Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]24
import numpy23
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]28
import numpy23
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]44
import numpy29
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]21____342
import numpy23
import numpy186
Matrix = [[ 5 10] [15 20]] The summation of elements= 50 The column wise summation= [20 30] The row wise summation= [15 35]48
import numpy23
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
589Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]29
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]21____348
import numpy23______6595
import numpy23____6597
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]29
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]20
Matrix1 = [[ 5 10] [15 20]] Matrix2 = [[25 30] [35 40]] Addition of two matrices: [[30 40] [50 60]] Subtraction of two matrices: [[-20 -20] [-20 -20]] Matrix Division: [[0.2 0.33333333] [0.42857143 0.5 ]] Multiplication of two matrices: [[125 300] [525 800]]21____6589
import numpy23
import numpy186
val = [['Dave',101,90,95],
['Alex',102,85,100],
['Ray',103,90,95]]
597import numpy23
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
406val = [['1001A','Ray', 'Technical Head'], ['2004B', 'Karlos' , 'Manager'], ['3100A', 'Alex' , 'Lead Developer']]
426Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
409n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
410_______421____7412
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]20
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
416import numpy202
import numpy24
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
419import numpy26
import numpy23
import numpy22
import numpy214
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]21
Matrix = [[ 5 10] [15 20]] The Transpose = [[ 5 15] [10 20]]22
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
426import numpy202
import numpy24
n = 3
m = 3
val = [0] * n
for x in range [n]:
val[x] = [0] * m
print[val]
419import numpy26
import numpy23
import numpy24
import numpy214
đầu ra
import numpy16
Xóa hàng và cột bằng Numpy
Ở đây, chúng tôi đang cố xóa các hàng bằng lệnh np. chức năng xóa []. Trong mã, đầu tiên chúng tôi cố gắng xóa hàng thứ 0, sau đó chúng tôi cố gắng xóa hàng thứ 2 và sau đó là hàng thứ 3