waltermilner

Untitled

Mar 26th, 2016
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4.   <script>
  5.     'use strict';
  6.  
  7.     // define Matrix type objects
  8.     function Matrix(values) // values is a 2D array
  9.     {
  10.         var m, rowCount, colCount;
  11.         this.m = values;
  12.         this.rowCount = values.length;
  13.         this.colCount = values[0].length; // assume is rectangular not ragged
  14.         // display function:
  15.         this.show = function ()
  16.         {
  17.             for (var row = 0; row < this.rowCount; row++)
  18.             {
  19.                 for (var col = 0; col < this.colCount; col++)
  20.                 {
  21.                     document.write(this.m[row][col] + " ");
  22.                 }
  23.                 document.writeln("<br>");
  24.             }
  25.             document.writeln("<br>");
  26.         };
  27.         // transpose function
  28.         this.transpose = function ()
  29.         {
  30.           // make array of arrays, transposed
  31.             var temp = new Array(this.colCount);
  32.             for (var col = 0; col < this.colCount; col++)
  33.             {
  34.                 temp[col] = new Array(rowCount);
  35.             }
  36.         // put values into 2D array
  37.         for (var row = 0; row < this.rowCount; row++)
  38.         {
  39.             for (var col = 0; col < this.colCount; col++)
  40.             {
  41.               temp[col][row]=this.m[row][col];
  42.             }
  43.  
  44.         }
  45.         // return new Matrix based on temp
  46.         return new Matrix(temp);
  47.       };
  48.  
  49.     }
  50.  
  51.     var m = [[1, 2], [3, 4], [5,6]];
  52.     var matrix1 = new Matrix(m);
  53.     var matrix2 = matrix1.transpose();
  54.     matrix2.show();
  55.     var matrix3=matrix2.transpose();
  56.     matrix3.show();
  57.   </script>
  58.   <head>
  59.     <title>Matrix</title>
  60.   </head>
  61.   <body>
  62.   </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment