Now matrices are widely used in image processing, genetic analysis, big data, and programming. The addition and subtraction of matrices are the two most common matrix operations. In this article, you’ll learn how to add and subtract two matrices.
Rules for Matrix Addition
Follow these rules to add two matrices:
Two matrices can only be added if they’re of the same order. If the two matrices are of the same order, add the corresponding elements of the two matrices i. e. , add the elements which have the same positions.
In example 1, the matrices can be added because they have the same order. In example 2, the matrices can’t be added because they don’t have the same order.
C++ Program to Add Two Matrices
Below is the C++ program to add two matrices:
Output:
Python Program to Add Two Matrices
Below is the Python program to add two matrices:
Output:
C Program to Add Two Matrices
Below is the C program to add two matrices:
Output:
JavaScript Program to Add Two Matrices
Below is the JavaScript program to add two matrices:
Output:
Rules for Matrix Subtraction
Follow these rules to subtract two matrices:
Two matrices can be subtracted only if they’re of the same order. If the two matrices are of the same order, subtract the corresponding elements of the two matrices i. e, subtract the elements which have the same positions.
In example 1, the matrices can be subtracted because they have the same order. In example 2, the matrices can’t be subtracted because they don’t have the same order.
C++ Program to Subtract Two Matrices
Below is the C++ program to subtract two matrices:
Output:
Python Program to Subtract Two Matrices
Below is the Python program to subtract two matrices:
Output:
C Program to Subtract Two Matrices
Below is the C program to subtract two matrices:
Output:
JavaScript Program to Subtract Two Matrices
Below is the JavaScript program to subtract two matrices:
Output:
If you want to have a look at the complete source code used in this article, here’s the GitHub repository.
Increase Your Programming Capability
You can increase your programming capability by practicing a variety of programming problems. Solving these programming problems helps you develop basic programming principles. These are a must-know if you’re to become an efficient programmer.