溫馨提示×

python如何讀取矩陣的行和列數(shù)

小億
309
2024-03-21 10:05:43
欄目: 編程語言

要讀取矩陣的行數(shù)和列數(shù),可以使用numpy庫中的shape屬性。下面是一個示例代碼:

import numpy as np

# 創(chuàng)建一個3x4的矩陣
matrix = np.array([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12]])

# 獲取矩陣的行數(shù)和列數(shù)
num_rows, num_cols = matrix.shape

# 打印行數(shù)和列數(shù)
print("矩陣的行數(shù)為:", num_rows)
print("矩陣的列數(shù)為:", num_cols)

運行上面的代碼,將輸出以下結(jié)果:

矩陣的行數(shù)為: 3
矩陣的列數(shù)為: 4

0