Advertisement
svetlai

Matrices

Jun 10th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // example:
  2. //[ [ 16, 32, 64, 128, 256, 512 ],
  3. //    [ 8, 16, 32, 64, 128, 256 ],
  4. //    [ 4, 8, 16, 32, 64, 128 ],
  5. //    [ 2, 4, 8, 16, 32, 64 ],
  6. //    [ 1, 2, 4, 8, 16, 32 ] ]
  7.  
  8. function fillMatrix(rows, cols){
  9.     var matrix = [];
  10.     for (var row = rows - 1; row >= 0; row -= 1) {
  11.         matrix[row] = [];
  12.         for (var col = 0; col < cols; col += 1) {
  13.             matrix[row][col] = Math.pow(2, col + (rows - row) - 1);
  14.         }
  15.     }
  16.  
  17.     return matrix;
  18. }
  19.  
  20. // example:
  21. //[ [ 15, 18, 21, 24, 27, 30, 33 ],
  22. //    [ 12, 15, 18, 21, 24, 27, 30 ],
  23. //    [ 9, 12, 15, 18, 21, 24, 27 ],
  24. //    [ 6, 9, 12, 15, 18, 21, 24 ],
  25. //    [ 3, 6, 9, 12, 15, 18, 21 ],
  26. //    [ 0, 3, 6, 9, 12, 15, 18 ] ]
  27.  
  28. function fillMatrix(rows, cols) {
  29.     var matrix = [];
  30.     for (var row = rows - 1; row >= 0; row -= 1) {
  31.         matrix[row] = [];
  32.         for (var col = 0; col < cols; col += 1) {
  33.             matrix[row][col] = (rows - 1 - row + col) * 3
  34.         }
  35.     }
  36.  
  37.     return matrix;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement