← All Exercises

Matrix Transpose

intermediatejavascript~15min

Background

Matrix transposition is fundamental in linear algebra, image processing, and machine learning data transformations.

Problem

Write a function that takes a 2D matrix (array of arrays) and returns its transpose, where rows become columns and columns become rows.

Input

A 2D array representing an m x n matrix with consistent row lengths.

Output

A 2D array representing the n x m transposed matrix.

Sample tests

Sample 1

Input: [[1,2,3],[4,5,6]]
Expected: [[1,4],[2,5],[3,6]]

Your solution

Learning objective

Understand 2D array indexing and practice functional matrix transformations.