PIXELBANKv9.1.0
Menu

Tensor Slicing and Indexing

Problem Statement

Given a 2D tensor, extract specific rows, columns, and sub-matrices using PyTorch indexing.

Background

PyTorch tensors support NumPy-style indexing and slicing, allowing you to extract arbitrary parts of a tensor.

Your Task

Write a function extract_parts(tensor) that takes a 4×4 tensor and returns a dictionary with its first row, last column, and center 2×2 sub-matrix.

Output Format

Return a dictionary with keys: "first_row", "last_column", "center".

Example:

Input:
torch.arange(16).reshape(4, 4)
Output:
{"first_row": [0, 1, 2, 3], "last_column": [3, 7, 11, 15], "center": [[5, 6], [9, 10]]}
Reasoning:

Use tensor[0], tensor[:, -1], and tensor[1:3, 1:3] for slicing

Constraints:

  • Input tensor is always 4x4
  • Values are integers
  • Return a dictionary with the three required keys
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Tensor Slicing and Indexing - Medium | PixelBank