Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <script>
- 'use strict';
- // define Matrix type objects
- function Matrix(values) // values is a 2D array
- {
- var m, rowCount, colCount;
- this.m = values;
- this.rowCount = values.length;
- this.colCount = values[0].length; // assume is rectangular not ragged
- // display function:
- this.show = function ()
- {
- for (var row = 0; row < this.rowCount; row++)
- {
- for (var col = 0; col < this.colCount; col++)
- {
- document.write(this.m[row][col] + " ");
- }
- document.writeln("<br>");
- }
- document.writeln("<br>");
- };
- // transpose function
- this.transpose = function ()
- {
- // make array of arrays, transposed
- var temp = new Array(this.colCount);
- for (var col = 0; col < this.colCount; col++)
- {
- temp[col] = new Array(rowCount);
- }
- // put values into 2D array
- for (var row = 0; row < this.rowCount; row++)
- {
- for (var col = 0; col < this.colCount; col++)
- {
- temp[col][row]=this.m[row][col];
- }
- }
- // return new Matrix based on temp
- return new Matrix(temp);
- };
- }
- var m = [[1, 2], [3, 4], [5,6]];
- var matrix1 = new Matrix(m);
- var matrix2 = matrix1.transpose();
- matrix2.show();
- var matrix3=matrix2.transpose();
- matrix3.show();
- </script>
- <head>
- <title>Matrix</title>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment