Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การหาจำนวนแถวและคอลัมน์ใน Matrix ที่กำหนดด้วย Numpy


เราจะสร้าง Numpy Matrix ก่อน แล้วจึงหาจำนวนแถวและคอลัมน์ใน Matrix นั้น

อัลกอริทึม

ขั้นตอนที่ 1: สร้าง Numpy Matrix จากตัวเลขสุ่ม
ขั้นตอนที่ 2: หาแถวและคอลัมน์ของ Matrix ด้วยฟังก์ชัน numpy.shape
ขั้นตอนที่ 3: แสดงจำนวนแถวและคอลัมน์

ตัวอย่างโค้ด

import numpy as np
matrix = np.random.rand(2,3)
print(matrix)
print("Total number of rows and columns in the given matrix are: ", matrix.shape)

ผลลัพธ์

[[0.23226052 0.89690884 0.19813164]  [0.85170808 0.97725669 0.72454096]]
Total number of rows and columns in the given matrix are:  (2, 3)