Reader Level:
ARTICLE

Transpose Matrix in TypeScript

Posted by Nitin Bhardwaj Articles | TypeScript November 19, 2012
In this article I will explain transpose matrix in TypeScript with an example.
  • 0
  • 0
  • 3302

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("<h2>Transpose Matrix in TypeScript</h2><hr>");

        m = parseInt(prompt("Enter the number of Rows of Matrix"));

        n = parseInt(prompt("Enter the number of Columns of Matrix"));

       

        document.write("<b>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("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

        } 

         for(i = 0; i < m; i++ )

            {

                for(j = 0; j < n; j++)

                {

                    document.write(" &nbsp;&nbsp;&nbsp;&nbsp;"+matrix[i][j]);

                }

            document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

            }

                 

        document.write("<br>");

        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("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

 

         for (i = 0; i < n; i++)

         {

             for (j = 0; j < m; j++)

             {

                 document.write("&nbsp;&nbsp;&nbsp;&nbsp;" + transpose[i][j]);

 

             }

             document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

         }

    }

}

window.onload = () => {

    var tramatrix = new Transpose_Matrix();

    tramatrix.matrix();

};


Demo.html

 

<!DOCTYPEhtml>

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>Transpose Matrix</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

    <h1>Transpose Matrix in TypeScript</h1>

    <divid="content"/>

</body>

</html>


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("<h2>Transpose Matrix in TypeScript</h2><hr>");

        m = parseInt(prompt("Enter the number of Rows of Matrix"));

        n = parseInt(prompt("Enter the number of Columns of Matrix"));

        document.write("<b>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("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

        }

        for(i = 0; i < m; i++) {

            for(j = 0; j < n; j++) {

                document.write(" &nbsp;&nbsp;&nbsp;&nbsp;" + matrix[i][j]);

            }

            document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

        }

        document.write("<br>");

        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("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

        for(i = 0; i < n; i++) {

            for(j = 0; j < m; j++) {

                document.write("&nbsp;&nbsp;&nbsp;&nbsp;" + transpose[i][j]);

            }

            document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

        }

    };

    return Transpose_Matrix;

})();

window.onload = function () {

    var tramatrix = new Transpose_Matrix();

    tramatrix.matrix();

};

 

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

COMMENT USING

Trending up