ARTICLE

Transpose Matrix in TypeScript

Posted by | November 19, 2012
In this article I will explain transpose matrix in TypeScript with an example.

Transpose Matrix in TypeScript

An array is an object that contains one or more items called elements. A two-dimensional array is really nothing more than an array of arrays. Although JavaScript doesn't provide for multi-dimensional arrays, you can get the same effect by creating an array of arrays. In an array of arrays, each element in the first array is another array. Suppose we have an A matrix. A Transpose Matrix is a simple tool used to convert the matrix A to transpose matrix At by interchanging rows and columns of the matrix A. In Transpose Matrix At, the row becomes a column and the column becomes a row by interchanging the index values of matrix A. When we transpose a matrix then the order of the matrix changes, but for a square matrix the order remains the same.

Example

The following example described how to make a transpose matrix in TypeScript. In this example prints transpose of a matrix. It is obtained by interchanging rows and columns of a matrix. It works as follows. For example, if the user entered an order as 2, 2 i.e. two rows and two columns and matrices as:

First Matrix Elements:
A   C
B   D
Transposed Matrix Elements:
A   B
C   D

To see how it works use the following instructions.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". After that, a window is opened. Enter the name of your application like "Transpose_Matrix", then click on the Ok button.

Step 2

After Step 1 your project has been created. Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, css file and html files.

Step 3

Transpose_matrix.ts

 class Transpose_Matrix {     matrix()     {         var matrix:number[][] = [[10],[10]];         var transpose:number[][] = [[10],[10]];         var i, j,m,n;         document.write("

Transpose Matrix in TypeScript

");         m = parseInt(prompt("Enter the number of Rows of Matrix"));         n = parseInt(prompt("Enter the number of Columns of Matrix"));                 document.write("1st Matrix Elements :");         for (i = 0; i < m+1; i++)         {             for (j = 0; j < n; j++)             {                 matrix[i][j] = parseInt(prompt("Enter the element of First Matrix -> Row " + (i) + " And Coloumn " + (j)));                            }             document.write("
");         }           for(i = 0; i < m; i++ )             {                 for(j = 0; j < n; j++)                 {                     document.write("     "+matrix[i][j]);                 }             document.write("
");             }                           document.write("
");         document.write("Transpose of Entered Matrix :");               for(i = 0; i < m; i++)         {            for(j = 0; j < n; j++)             {                transpose[j][i] = matrix[i][j];             }         }                document.write("
");            for (i = 0; i < n; i++)          {              for (j = 0; j < m; j++)              {                  document.write("    " + transpose[i][j]);                }              document.write("
");          }     } } window.onload = () => {     var tramatrix = new Transpose_Matrix();     tramatrix.matrix(); };

Demo.html

         Transpose Matrix

Transpose Matrix in TypeScript

app.js

 var Transpose_Matrix = (function () {     function Transpose_Matrix() { }     Transpose_Matrix.prototype.matrix = function () {         var matrix = [             [                 20             ],             [                 20             ]         ];         var transpose = [             [                 20             ],             [                 20             ]         ];         var i;         var j;         var m;         var n;           document.write("

Transpose Matrix in TypeScript

");         m = parseInt(prompt("Enter the number of Rows of Matrix"));         n = parseInt(prompt("Enter the number of Columns of Matrix"));         document.write("1st Matrix Elements :");         for(i = 0; i < m; i++) {             for(j = 0; j < n; j++) {                 matrix[i][j] = parseInt(prompt("Enter the element of First Matrix -> Row " + (i) + " And Coloumn " + (j)));             }             document.write("
");         }         for(i = 0; i < m; i++) {             for(j = 0; j < n; j++) {                 document.write("     " + matrix[i][j]);             }             document.write("
");         }         document.write("
");         document.write("Transpose of Entered Matrix :");         for(i = 0; i < m; i++) {             for(j = 0; j < n; j++) {                 transpose[j][i] = matrix[i][j];             }         }         document.write("
");         for(i = 0; i < n; i++) {             for(j = 0; j < m; j++) {                 document.write("    " + transpose[i][j]);             }             document.write("
");         }     };     return Transpose_Matrix; })(); window.onload = function () {     var tramatrix = new Transpose_Matrix();     tramatrix.matrix(); };

Output 1

Enter the number of Rows and Columns.

Output 2

Enter the element of the first matrix.

Output 3

After Step 2, the transposition of the matrix is displayed.