Transpose Matrix in TypeScript

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 to 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
  1. class Transpose_Matrix {  
  2.  matrix() {  
  3.   var matrix: number[][] = [  
  4.    [10],  
  5.    [10]  
  6.   ];  
  7.   var transpose: number[][] = [  
  8.    [10],  
  9.    [10]  
  10.   ];  
  11.   var i, j, m, n;  
  12.   document.write("<h2>Transpose Matrix in TypeScript</h2><hr>");  
  13.   m = parseInt(prompt("Enter the number of Rows of Matrix"));  
  14.   n = parseInt(prompt("Enter the number of Columns of Matrix"));  
  15.   document.write("<b>1st Matrix Elements :");  
  16.   for (i = 0; i < m + 1; i++) {  
  17.    for (j = 0; j < n; j++) {  
  18.     matrix[i][j] = parseInt(prompt("Enter the element of First Matrix -> Row " + (i) + " And Coloumn " + (j)));  
  19.    }  
  20.    document.write("<br>    ");  
  21.   }  
  22.   for (i = 0; i < m; i++) {  
  23.    for (j = 0; j < n; j++) {  
  24.     document.write("     " + matrix[i][j]);  
  25.    }  
  26.    document.write("<br>    ");  
  27.   }  
  28.   document.write("<br>");  
  29.   document.write("Transpose of Entered Matrix :");  
  30.   for (i = 0; i < m; i++) {  
  31.    for (j = 0; j < n; j++) {  
  32.     transpose[j][i] = matrix[i][j];  
  33.    }  
  34.   }  
  35.   document.write("<br>    ");  
  36.   for (i = 0; i < n; i++) {  
  37.    for (j = 0; j < m; j++) {  
  38.     document.write("    " + transpose[i][j]);  
  39.    }  
  40.    document.write("<br>    ");  
  41.   }  
  42.  }  
  43. }  
  44. window.onload = () => {  
  45.  var tramatrix = new Transpose_Matrix();  
  46.  tramatrix.matrix();  
  47. };  
Demo.html
  1. <!DOCTYPEhtml>  
  2. <htmllang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <metacharset="utf-8"/>  
  6.         <title>Transpose Matrix</title>  
  7.         <linkrel="stylesheet"href="app.css"type="text/css"/>  
  8.         <scriptsrc="app.js">  
  9.         </script>  
  10.     </head>  
  11.     <body>  
  12.         <h1>Transpose Matrix in TypeScript</h1>  
  13.         <divid="content"/>  
  14.     </body>  
  15. </html> 
app.js
  1. var Transpose_Matrix = (function() {  
  2.  function Transpose_Matrix() {}  
  3.  Transpose_Matrix.prototype.matrix = function() {  
  4.   var matrix = [  
  5.    [20],  
  6.    [20]  
  7.   ];  
  8.   var transpose = [  
  9.    [20],  
  10.    [20]  
  11.   ];  
  12.   var i;  
  13.   var j;  
  14.   var m;  
  15.   var n;  
  16.   document.write("<h2>Transpose Matrix in TypeScript</h2><hr>");  
  17.   m = parseInt(prompt("Enter the number of Rows of Matrix"));  
  18.   n = parseInt(prompt("Enter the number of Columns of Matrix"));  
  19.   document.write("<b>1st Matrix Elements :");  
  20.   for (i = 0; i < m; i++) {  
  21.    for (j = 0; j < n; j++) {  
  22.     matrix[i][j] = parseInt(prompt("Enter the element of First Matrix -> Row " + (i) + " And Coloumn " + (j)));  
  23.    }  
  24.    document.write("<br>    ");  
  25.   }  
  26.   for (i = 0; i < m; i++) {  
  27.    for (j = 0; j < n; j++) {  
  28.     document.write("     " + matrix[i][j]);  
  29.    }  
  30.    document.write("<br>    ");  
  31.   }  
  32.   document.write("<br>");  
  33.   document.write("Transpose of Entered Matrix :");  
  34.   for (i = 0; i < m; i++) {  
  35.    for (j = 0; j < n; j++) {  
  36.     transpose[j][i] = matrix[i][j];  
  37.    }  
  38.   }  
  39.   document.write("<br>    ");  
  40.   for (i = 0; i < n; i++) {  
  41.    for (j = 0; j < m; j++) {  
  42.     document.write("    " + transpose[i][j]);  
  43.    }  
  44.    document.write("<br>    ");  
  45.   }  
  46.  };  
  47.  return Transpose_Matrix;  
  48. })();  
  49. window.onload = function() {  
  50.  var tramatrix = new Transpose_Matrix();  
  51.  tramatrix.matrix();  
  52. };  
Output 1
Enter the number of Rows and Columns.
 
enter-row.jpg
 
enter-column.jpg
 
Output 2
Enter the element of the first matrix.
 
enter-element-1.jpg
 
enter-element-2.jpg
  
enter-element-3.jpg
  
enter-element-4.jpg
 
Output 3
After Step 2, the transposition of the matrix is displayed.
 
final-result.jpg


Similar Articles