Editor

Matrix Extraction

Intermediate · Write the code · from NumPy Arrays: Create, Index, Slice, and Reshape

From the lesson: NumPy Arrays: Create, Index, Slice, and Reshape

Imagine you have a spreadsheet with a million rows of numbers and you need to multiply every single value by 2. With a regular Python list, you'd write a loop that visits each cell one by one. It works, but it's slow.

Read the lesson

Create a 4x4 matrix of numbers 1 through 16 using arange and reshape. Then print:

1. The entire second row

2. The entire third column

3. The 2x2 submatrix from the bottom-right corner

import numpy as np
# Create 4x4 matrix (1 to 16)
# Print second row, third column, bottom-right 2x2

More NumPy Array exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz