Advertisement
Guest User

YT ASMR Matrix addition and subtraction source code

a guest
Sep 7th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. public class MatrixAddSubtract {
  2.  
  3.  
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  
  8. int[][] matrixA = new int[][]{{3,6,10,-1,4}, {6, 10, 5, 3, 1}, {4,4,10,40,-10}};
  9. int[][] matrixB = new int[][]{{1,-5, 0, 0, 0}, {2, 0, -14, 3, 2}, {3,3,10,100,-30}};
  10.  
  11. System.out.println("\n\nMatrix A:");
  12. printMatrix(matrixA);
  13. System.out.println("\n\nMatrix B:");
  14. printMatrix(matrixB);
  15. System.out.println(checkIfRectangular(matrixA));
  16.  
  17. System.out.println("Subtracting A + B");
  18. int[][] resultAPlusB = subtractMatrixAB(matrixA, matrixB);
  19. printMatrix(resultAPlusB);
  20.  
  21. }
  22.  
  23.  
  24. //adds A and B
  25. public static int[][] addMatrixAB(int[][] a, int[][] b) {
  26.  
  27. int[][] resultingMatrix = new int[a.length][a[0].length];
  28. //validation
  29. //A and B both need to be m x n in dimensions or else it would not work
  30. if ((a.length == b.length) && (checkIfRectangular(a)) && checkIfRectangular(b)) {
  31.  
  32. for (int i = 0; i < a.length; i++) {
  33.  
  34. for (int j = 0; j < a[i].length; j++) {
  35. resultingMatrix[i][j] = a[i][j] + b[i][j];
  36. }
  37. }
  38. }
  39. else {
  40. System.out.println("error: Matrix must have the same dimensions / each matrix must have equal columns");
  41. return new int[][] {{4}};
  42. }
  43. return resultingMatrix;
  44.  
  45. }
  46.  
  47.  
  48. //subtracts B from A
  49.  
  50. public static int[][] subtractMatrixAB(int[][] a, int[][] b) {
  51.  
  52. int[][] resultingMatrix = new int[a.length][a[0].length];
  53. //validation
  54. //A and B both need to be m x n in dimensions or else it would not work
  55. if ((a.length == b.length) && (checkIfRectangular(a)) && checkIfRectangular(b)) {
  56.  
  57. for (int i = 0; i < a.length; i++) {
  58.  
  59. for (int j = 0; j < a[i].length; j++) {
  60. resultingMatrix[i][j] = a[i][j] - b[i][j];
  61. }
  62. }
  63. }
  64. else {
  65. System.out.println("error: Matrix must have the same dimensions / each matrix must have equal columns");
  66. return new int[][] {{4}};
  67. }
  68. return resultingMatrix;
  69.  
  70. }
  71.  
  72.  
  73. private static boolean checkIfRectangular(int[][] x) {
  74.  
  75. //get length of the first subarray
  76. int subArraylength = x[0].length;
  77.  
  78. for (int i = 0; i < x.length; i++) {
  79. if (x[i].length != subArraylength) {
  80. return false;
  81. }
  82. }
  83.  
  84. return true;
  85.  
  86. }
  87.  
  88.  
  89. public static void printMatrix(int[][] x) {
  90. for (int[] i : x) {
  91.  
  92. for (int j : i) {
  93. System.out.print(j + "\t");
  94. }
  95. System.out.println("\n");
  96.  
  97. }
  98. }
  99.  
  100.  
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement