How to Check the Size of a Matrix in Python?

Estimated read time 1 min read

To check the size of a matrix in Python, you can use the NumPy library. NumPy provides a data structure called ndarray that is commonly used to represent matrices and multi-dimensional arrays. Here’s an example of how you can determine the size of a matrix using NumPy:

import numpy as np

# Create a matrix
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Get the size of the matrix
matrix_size = matrix.shape

# Print the size of the matrix

In the code above, we first import the NumPy library using the import statement. Then, we create a matrix using the np.array() function. Next, the shape attribute of the matrix is accessed using the matrix.shape syntax, which returns a tuple representing the dimensions of the matrix. Finally, we print the size of the matrix using the print() function.

The output will be (3, 3), indicating that the matrix has 3 rows and 3 columns.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply