Lesson 3
Matrix Transposition with TypeScript
Introduction

Hello there! Are you ready to enhance your programming skills with TypeScript? In this unit, we will delve into the world of matrices. More specifically, we'll be transposing a given matrix. Let's dive into this matrix manipulation exercise without delay!

Task Statement

To begin, let's elaborate on the task at hand: we will write a TypeScript function named transformMatrix. This function will accept a 2D array of integers as input, representing a matrix. Your goal is to return another 2D array, which is the transposed version of the given matrix.

When we mention transposing a matrix, we are referring to the process of switching its rows and columns. In other words, all the rows of the original matrix should become columns in the transposed matrix, and vice versa.

For instance, if the original matrix (input 2D array) is:

TypeScript
1let matrix: number[][] = [ 2 [1, 2, 3], 3 [4, 5, 6] 4];

Then the transposed matrix (output 2D array) will be:

TypeScript
1let transposed: number[][] = [ 2 [1, 4], 3 [2, 5], 4 [3, 6] 5];

It is vital that your result maintains the integrity of the data types present in the original matrix. The values in the input matrix are integers, and they should remain integers in the output matrix as well.

Step 1: Determine the Dimensions of the Matrix

The initial step in building our solution involves determining the dimensions of the matrix. We need to know the number of rows and columns present in it. In TypeScript, we can easily determine the dimensions, ensuring that type safety is maintained.

TypeScript
1function transformMatrix(matrix: number[][]): number[][] { 2 const rows: number = matrix.length; 3 const cols: number = rows > 0 ? matrix[0].length : 0; 4 // If rows > 0, the above line sets cols to 5 // the length of the first row, else sets to 0 6}
Step 2: Create a Placeholder for the Transposed Matrix

The subsequent step is to create a placeholder for the transposed matrix that aligns with its required dimensions. This will be a new 2D array, but with the number of rows and columns swapped.

TypeScript
1function transformMatrix(matrix: number[][]): number[][] { 2 const rows: number = matrix.length; 3 const cols: number = rows > 0 ? matrix[0].length : 0; 4 const result: number[][] = []; 5 6 for (let i: number = 0; i < cols; ++i) { 7 result[i] = []; 8 } 9}

In this code snippet, we create a new array object and populate it with empty arrays, one row for each column.

Step 3: Transpose the Matrix

It's time to get to the crux of the matter — transposing the matrix. For every element in the original matrix, we want to move it from the ith row and jth column to the jth row and ith column of the transposed matrix.

TypeScript
1function transformMatrix(matrix: number[][]): number[][] { 2 const rows: number = matrix.length; 3 const cols: number = rows > 0 ? matrix[0].length : 0; 4 const result: number[][] = []; 5 6 for (let i: number = 0; i < cols; ++i) { 7 result[i] = []; 8 for (let j: number = 0; j < rows; ++j) { 9 result[i][j] = matrix[j][i]; 10 } 11 } 12 return result; 13} 14 15let matrix: number[][] = [ 16 [1, 2, 3], 17 [4, 5, 6] 18]; 19 20let transposed: number[][] = transformMatrix(matrix); 21 22for (let row of transposed) { 23 console.log(row.join(' ')); 24} 25 26// Outputs: 27// 1 4 28// 2 5 29// 3 6

A straightforward nested for loop can effortlessly execute this swap for all elements of the matrix. Immediately after we create the new row, we populate it with the values using another for loop, one column for each row in the original matrix.

Lesson Summary

With this, we've brought this lesson to a close! Congratulations on successfully implementing a function that can transpose matrices using TypeScript! By accomplishing this task, you've demonstrated your understanding and proficiency in TypeScript's handling of multidimensional arrays, type safety, and the concept of matrix transposition.

Your hard work continues, though. Now that you have acquired this valuable skill, it's time to reinforce it with more practice. During the next session, you will encounter practice problems that build on this concept. So, get ready and start coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.