Ma trận không là gì ngoài sự sắp xếp hình chữ nhật của dữ liệu hoặc số. Nói cách khác, nó là một mảng dữ liệu hoặc số hình chữ nhật. Các mục ngang trong một ma trận được gọi là ‘hàng, trong khi các mục dọc được gọi là‘ cột. Nếu một ma trận có số r số hàng và số C của các cột thì thứ tự của ma trận được đưa ra bởi r x c. Mỗi mục trong một ma trận có thể là các giá trị số nguyên hoặc giá trị nổi, hoặc thậm chí nó có thể là các số phức tạp.r x c. Each entries in a matrix can be integer values, or floating values, or even it can be complex numbers.
Examples:
// 3 x 4 matrix 1 2 3 4 M = 4 5 6 7 6 7 8 9 // 2 x 3 matrix in Python A = [ [ 2, 5, 7 ], [ 4, 7, 9 ] ] // 3 x 4 matrix in Python where entries are floating numbers B = [ [ 1.0, 3.5, 5.4, 7.9 ], [ 9.0, 2.5, 4.2, 3.6 ], [ 1.5, 3.2, 1.6, 6.5 ] ]
Trong Python, chúng ta có thể lấy ma trận đầu vào của người dùng theo những cách khác nhau. Một số phương pháp cho ma trận đầu vào của người dùng trong Python được hiển thị bên dưới:
Mã số 1:
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 61
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 62
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 63
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 65
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64__17171718
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 69
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 62
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 63
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 65
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]5
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 68
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]7
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 62
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]9
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
0Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
2import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
3import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
5import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
6 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
7import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
8import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
9[[1 2 3]
[3 4 5]
[7 8 9]]
0Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6229
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
9import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 [[1 2 3]
[3 4 5]
[7 8 9]]
5import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
6 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
7[[1 2 3]
[3 4 5]
[7 8 9]]
8[[1 2 3]
[3 4 5]
[7 8 9]]
9import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
for line in a:
print [' '.join[map[str, line]]]
0Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 63
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 65
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
for line in a:
print [' '.join[map[str, line]]]
4import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
9import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
for line in a:
print [' '.join[map[str, line]]]
6import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
5import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
6 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
71 2 3
3 4 5
7 8 9
1import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
9import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 [[1 2 3]
[3 4 5]
[7 8 9]]
5import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
6 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
71 2 3
3 4 5
7 8 9
71 2 3
3 4 5
7 8 9
8import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
0 ____70Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 62
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print['\n'.join[[''.join[['{:4}'.format[item] for item in row]]
for row in a]]]
2import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
3import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
9import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
0import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print['\n'.join[[''.join[['{:4}'.format[item] for item in row]]
for row in a]]]
6Output:
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6
Lót:
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print['\n'.join[[''.join[['{:4}'.format[item] for item in row]]
for row in a]]]
7Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 62
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print['\n'.join[[''.join[['{:4}'.format[item] for item in row]]
for row in a]]]
9Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 63__
& nbsp; Mã số 2: Sử dụng hàm
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
4 và import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
5.Code #2: Using
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
4 function and import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
5.Trong Python, tồn tại một thư viện phổ biến tên là Numpy. Thư viện này là một thư viện cơ bản cho bất kỳ tính toán khoa học nào. Nó cũng được sử dụng cho các mảng đa chiều và như chúng ta biết ma trận là một mảng hình chữ nhật, chúng ta sẽ sử dụng thư viện này cho ma trận đầu vào của người dùng.NumPy. This library is a fundamental library for any scientific computation. It is also used for multidimensional arrays and as we know matrix is a rectangular array, we will use this library for user input matrix.
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
6 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
7Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 61
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 62
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 63
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 65
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64__17171718
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 69
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 62
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 63
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 65
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]5
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 68
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
0Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 64
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 616
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
3Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]7
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 62
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]9
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
5import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
6 import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
7import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
8import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
0Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 632
Output:
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]
Tạo: Tháng 3-20, 2021 | Cập nhật: Tháng 5-13, 2021 Danh sách hiểu biết cung cấp một cách ngắn gọn và thanh lịch để làm việc với các danh sách trong một dòng mã duy nhất. Phương pháp này cũng sử dụng vòng lặp
4 để in ma trận trong Pythonimport numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 nhưng được coi là nhanh hơn một chút so với việc sử dụng nó theo truyền thống, như trong phương pháp trước đó.import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
Lấy mã sau làm ví dụ.
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
Output:
[[1 2 3]
[3 4 5]
[7 8 9]]
Mặc dù mã trên sử dụng gần như các chức năng giống như trong phương thức trước, nhưng nó thực hiện hiệu quả hơn và trong một dòng. Sự khác biệt chính khác là hàm
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 639 được sử dụng ở đây để cung cấp khoảng cách cần thiết giữa các phần tử chuỗi.
Sử dụng vòng import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 để in ma trận trong Python
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
Phương pháp này sẽ lặp lại thông qua ma trận bằng cách sử dụng vòng lặp
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 và in từng hàng riêng lẻ sau khi định dạng đúng. Mã sau đây cho thấy cách.import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
for line in a:
print [' '.join[map[str, line]]]
Output:
1 2 3
3 4 5
7 8 9
Chúng tôi sử dụng hàm
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 622, chuyển đổi toàn bộ hàng thành một chuỗi và sau đó chúng tôi áp dụng chức năng
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 637 cho toàn bộ hàng này chuyển đổi tất cả thành một chuỗi và các phần tử riêng biệt theo trình phân tách được chỉ định.
Sử dụng phương pháp hiểu danh sách để in ma trận trong Python
Danh sách hiểu biết cung cấp một cách ngắn gọn và thanh lịch để làm việc với các danh sách trong một dòng mã duy nhất.
Phương pháp này cũng sử dụng vòng lặp
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print[a]
4 nhưng được coi là nhanh hơn một chút so với việc sử dụng nó theo truyền thống, như trong phương pháp trước đó.Lấy mã sau làm ví dụ.
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
print['\n'.join[[''.join[['{:4}'.format[item] for item in row]]
for row in a]]]
Output:
1 2 3
3 4 5
7 8 9
Mặc dù mã trên sử dụng gần như các chức năng giống như trong phương thức trước, nhưng nó thực hiện hiệu quả hơn và trong một dòng. Sự khác biệt chính khác là hàm
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 639 được sử dụng ở đây để cung cấp khoảng cách cần thiết giữa các phần tử chuỗi.
Một cách khác để sử dụng phương pháp này được hiển thị dưới đây. Ở đây chúng tôi chia mã riêng thành các dòng riêng lẻ và cố gắng làm cho ma trận giống như cấu trúc giống như bảng.
import numpy as np
a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
Output:
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 60