Creating a 3×3 Matrix

Write a NumPy program to create a 3×3 matrix with values ranging from 2 to 10. Return the matrix.

Example 1:

Input: None 

Output: array([[2, 3, 4], [5, 6, 7], [8, 9, 10]])

Example 2:

Input: None 

Output: array([[2, 3, 4], [5, 6, 7], [8, 9, 10]])

You can use np.arange() function to generate a range of numbers and np.reshape() function to reshape the array into a 3×3 matrix.

import numpy as np

def create_matrix():
    return np.arange(2, 11).reshape(3,3)

print(create_matrix())

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!