{"id":7062,"date":"2021-09-30T16:30:14","date_gmt":"2021-09-30T11:00:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=7062"},"modified":"2021-11-22T18:39:24","modified_gmt":"2021-11-22T13:09:24","slug":"program-to-subtract-two-matrices","status":"publish","type":"post","link":"https:\/\/python-programs.com\/program-to-subtract-two-matrices\/","title":{"rendered":"Program to Subtract two Matrices in Python & C++ Programming"},"content":{"rendered":"
What is a matrix:<\/strong><\/p>\n A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Here as there are 5 rows and 4 columns it is called as 5*4 matrix.<\/p>\n What is matrix subtraction:<\/strong><\/p>\n Given two matrices of same order(dimensions) then we can easily add the two matrices by doing the difference of corresponding elements in both the matrices.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n <\/p>\n Here Matrix C is matrix subtraction\u00a0 of the matrices A and B.<\/p>\n Examples for matrix addition:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n A matrix can be implemented as a nested list in Python (list inside a list). Each variable can be thought of as a row in the matrix.<\/p>\n Approach:<\/strong><\/p>\n Below is the\u00a0 implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The program’s output is the same as previously. For iterating through each element in the array, we used the nested list comprehension.<\/p>\n Understanding the list helps us to write concise codes and we need to always try to use them in Python. They’re really beneficial.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We used nesting loops in this program to iterate through and row and column. In the two matrices we will subract the appropriate elements and store them in the result at each level.<\/p>\n Let us take dynamic input in this case.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n What is a matrix: A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix. Example: Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20. In this order, the dimensions of …<\/p>\nMatrix 1 = 2 3 1\r\n 1 2 3\r\n -3 0 2 \r\nMatrix 2 = 1 4 2\r\n -4 5 -1\r\n 2 1 4<\/pre>\n
Printing the difference of matrices : \r\n1 -1 -1\r\n5 -3 4\r\n-5 -1 -2<\/pre>\n
Here we subtracted the corresponding elements in both matrices<\/pre>\n
Program for Matrix Subtraction<\/h2>\n
\n
Method #1:Using nested Loops in Python<\/h3>\n
\n
\nDeclare a new array diff, this time with dimensions in rows and columns.<\/li>\n# given matrix A\r\nA = [[2, 3, 1],\r\n [1, 2, 3],\r\n [-3, 0, 2]]\r\n# given matrix B\r\nB = [[1, 4, 2],\r\n [-4, 5, -1],\r\n [2, 1, 4]]\r\n# Initialize the difference of matrices elements to 0\r\nmatrixDiff = [[0, 0, 0],\r\n [0, 0, 0],\r\n [0, 0, 0]]\r\n# Traverse the rows\r\nfor rows in range(len(A)):\r\n # Traverse the columns\r\n for columns in range(len(A[0])):\r\n matrixDiff[rows][columns] = A[rows][columns] - B[rows][columns]\r\n# printing the difference of matrices\r\nprint(\"Printing the difference of matrices : \")\r\nfor rows in matrixDiff:\r\n print(*rows)\r\n<\/pre>\n
Printing the difference of matrices : \r\n1 -1 -1\r\n5 -3 4\r\n-5 -1 -2<\/pre>\n
Method #2:Using List Comprehension in Python<\/h3>\n
# given matrix A\r\nA = [[2, 3, 1],\r\n [1, 2, 3],\r\n [-3, 0, 2]]\r\n# given matrix B\r\nB = [[1, 4, 2],\r\n [-4, 5, -1],\r\n [2, 1, 4]]\r\n# Initialize the difference of matrices elements to 0\r\nmatrixDiff = [[0, 0, 0],\r\n [0, 0, 0],\r\n [0, 0, 0]]\r\n\r\n# using list comprehension\r\nmatrixDiff = [[A[i][j] - B[i][j]\r\n for j in range(len(A[0]))] for i in range(len(A))]\r\n\r\n# printing the difference of matrices\r\nprint(\"Printing the difference of matrices : \")\r\nfor rows in matrixDiff:\r\n print(*rows)\r\n<\/pre>\n
Printing the sum of matrices : \r\n16 -1 -5\r\n6 11 6<\/pre>\n
Method #3:Using nested loops in C++<\/h3>\n
#include <iostream>\r\nusing namespace std;\r\nint main()\r\n{\r\n int rows, columns, A[100][100], B[100][100], DiffMatrix[100][100], i, j;\r\n cout << \"Enter the number of rows of the matrix \"<<endl;\r\n cin >> rows;\r\n cout << \"Enter the number of columns of the matrix\"<<endl;\r\n cin >> columns;\r\n cout << \"Enter the elements of first matrix \" << endl;\r\n \/\/ Initializing matrix A with the user defined values\r\n for(i = 0; i < rows; ++i)\r\n for(j = 0; j < columns; ++j)\r\n {\r\n cout << \"Enter element A\" << i + 1 << j + 1 << \" = \";\r\n cin >> A[i][j];\r\n }\r\n \/\/ Initializing matrix B with the user defined values\r\n cout << endl << \"Enter elements of 2nd matrix: \" << endl;\r\n for(i = 0; i < rows; ++i)\r\n for(j = 0; j < columns; ++j)\r\n {\r\n cout << \"Enter element B\" << i + 1 << j + 1 << \" = \";\r\n cin >> B[i][j];\r\n }\r\n \/\/ Performing matrix addition by doing diffeerence of given two matrices A and B\r\n for(i = 0; i < rows; ++i)\r\n for(j = 0; j < columns; ++j)\r\n DiffMatrix[i][j] = A[i][j] - B[i][j];\r\n \/\/printing matrix A\r\n cout << endl << \" printing the matrix A\" << endl;\r\n for (i = 0; i < rows; ++i) {\r\n for (j = 0; j < columns; ++j) {\r\n cout << A[i][j] << \" \";\r\n }\r\n cout << endl;\r\n}\r\n \/\/printing matrix B\r\n cout << endl << \" printing the matrix B\" << endl;\r\n for (i = 0; i < rows; ++i) {\r\n for (j = 0; j < columns; ++j) {\r\n cout << B[i][j] << \" \";\r\n }\r\n cout << endl;\r\n}\r\n \/\/printing the difference of matrices\r\n cout << endl << \" printing the difference of matrices A and B\" << endl;\r\n \r\n for (i = 0; i < rows; ++i) {\r\n for (j = 0; j < columns; ++j) {\r\n cout << DiffMatrix[i][j] << \" \";\r\n }\r\n cout << endl;\r\n}\r\n return 0;\r\n}<\/pre>\n
Enter the number of rows of the matrix \r\n4\r\nEnter the number of columns of the matrix\r\n4\r\nEnter the elements of first matrix \r\nEnter element A11 = 1\r\nEnter element A12 = 2\r\nEnter element A13 = 3\r\nEnter element A14 = 4\r\nEnter element A21 = 5\r\nEnter element A22 = 6\r\nEnter element A23 = 5\r\nEnter element A24 = 2\r\nEnter element A31 = -5\r\nEnter element A32 = 9\r\nEnter element A33 = 1\r\nEnter element A34 = 5\r\nEnter element A41 = 0\r\nEnter element A42 = 5\r\nEnter element A43 = 2\r\nEnter element A44 = 5\r\n\r\nEnter elements of 2nd matrix: \r\nEnter element B11 = 1\r\nEnter element B12 = 9\r\nEnter element B13 = 5\r\nEnter element B14 = 3\r\nEnter element B21 = 8\r\nEnter element B22 = -4\r\nEnter element B23 = -8\r\nEnter element B24 = 0\r\nEnter element B31 = 5\r\nEnter element B32 = 1\r\nEnter element B33 = 7\r\nEnter element B34 = -3\r\nEnter element B41 = -8\r\nEnter element B42 = 1\r\nEnter element B43 = 2\r\nEnter element B44 = 0\r\n\r\nprinting the matrix A\r\n1 2 3 4 \r\n5 6 5 2 \r\n-5 9 1 54 \r\n0 5 2 5\r\n\r\nprinting the matrix B\r\n1 9 5 3 \r\n8 -4 -38 0 \r\n5 1 7 -3 \r\n-8 1 2 0\r\n\r\nprinting the difference of matrices A and B\r\n0 -7 -2 1 \r\n-3 10 43 2 \r\n-10 8 -6 57 \r\n8 4 0 5<\/pre>\n
\n