- What is matrix subtraction and its conditions
- How to subtract two matrices using nested loops
- How to handle matrices with different dimensions
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to subtract two matrices.
Matrix subtraction is a simple operation where we subtract corresponding elements of two matrices. It is a fundamental operation in linear algebra and is used in many applications.
š” Key Point: Matrix subtraction is possible only when both matrices have the same dimensions (same number of rows and columns).
Matrix Subtraction Rule
- Matrix A: Dimensions
r Ć c - Matrix B: Dimensions
r Ć c(must match) - Result Matrix C: Dimensions
r Ć c - Formula:
C[i][j] = A[i][j] - B[i][j]
Formula:
C[i][j] = A[i][j] - B[i][j]
Each element of the result matrix is the difference of corresponding elements from A and B.
Visual Example
Matrix A (2Ć3):
[1 2 3] [4 5 6]
Matrix B (2Ć3):
[7 8 9] [10 11 12]
Result C (2Ć3):
[-6 -6 -6] [-6 -6 -6]
Calculation: C[0][0] = 1 - 7 = -6
C[0][1] = 2 - 8 = -6
C[0][2] = 3 - 9 = -6
C[1][0] = 4 - 10 = -6
C[1][1] = 5 - 11 = -6
C[1][2] = 6 - 12 = -6
Matrix subtraction is used in many real-world applications, such as:
- Image Processing: Difference between two images
- Data Analysis: Finding differences between datasets
- Physics: Subtracting vectors and matrices
- Computer Graphics: Transformations and comparisons
C Program for Matrix Subtraction
#include <stdio.h>
int main() {
int rows, cols, i, j;
// Ask user for matrix dimensions
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Declare matrices
int A[rows][cols], B[rows][cols], C[rows][cols];
// ====== READ FIRST MATRIX ======
printf("\nEnter %d elements for first matrix:\n", rows * cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("A[%d][%d] = ", i, j);
scanf("%d", &A[i][j]);
}
}
// ====== READ SECOND MATRIX ======
printf("\nEnter %d elements for second matrix:\n", rows * cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("B[%d][%d] = ", i, j);
scanf("%d", &B[i][j]);
}
}
// ====== MATRIX SUBTRACTION ======
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
// ====== DISPLAY MATRICES ======
printf("\n=== First Matrix (%dĆ%d) ===\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("\n=== Second Matrix (%dĆ%d) ===\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
printf("\n=== Result Matrix (%dĆ%d) ===\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
Sample Output
Enter the number of rows: 2 Enter the number of columns: 3 Enter 6 elements for first matrix: A[0][0] = 10 A[0][1] = 20 A[0][2] = 30 A[1][0] = 40 A[1][1] = 50 A[1][2] = 60 Enter 6 elements for second matrix: B[0][0] = 7 B[0][1] = 8 B[0][2] = 9 B[1][0] = 10 B[1][1] = 11 B[1][2] = 12 === First Matrix (2Ć3) === 10 20 30 40 50 60 === Second Matrix (2Ć3) === 7 8 9 10 11 12 === Result Matrix (2Ć3) === 3 12 21 30 39 48
Example with Square Matrices:
Enter the number of rows: 3 Enter the number of columns: 3 Enter 9 elements for first matrix: A[0][0] = 9 A[0][1] = 8 A[0][2] = 7 A[1][0] = 6 A[1][1] = 5 A[1][2] = 4 A[2][0] = 3 A[2][1] = 2 A[2][2] = 1 Enter 9 elements for second matrix: B[0][0] = 1 B[0][1] = 2 B[0][2] = 3 B[1][0] = 4 B[1][1] = 5 B[1][2] = 6 B[2][0] = 7 B[2][1] = 8 B[2][2] = 9 === First Matrix (3Ć3) === 9 8 7 6 5 4 3 2 1 === Second Matrix (3Ć3) === 1 2 3 4 5 6 7 8 9 === Result Matrix (3Ć3) === 8 6 4 2 0 -2 -4 -6 -8
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int rows, cols;ā dimensions of the matricesint i, j;ā loop counters
- Declare Matrices:
int A[rows][cols];ā first matrixint B[rows][cols];ā second matrixint C[rows][cols];ā result matrix
- Read Matrices: Uses nested loops to read elements into both matrices.
- Matrix Subtraction: The nested loops subtract corresponding elements and store the result in
C[i][j]. - Display Matrices: Prints all three matrices.
- Return:
return 0;indicates successful program execution.
š Note: The matrices must have the same dimensions for subtraction to work. If the dimensions differ, the program will not produce correct results.
Algorithm for Matrix Subtraction
Step-by-step algorithm:
- Start
- Read the number of rows and columns
- Read elements of first matrix (A)
- Read elements of second matrix (B)
- For
i = 0torows-1:- For
j = 0tocols-1:C[i][j] = A[i][j] - B[i][j]
- For
- Print the result matrix
- End
Subtracting Matrices with Dimension Validation
A more robust program checks if the matrices have the same dimensions before performing subtraction:
#include <stdio.h>
int main() {
int r1, c1, r2, c2, i, j;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
// Check if subtraction is possible
if(r1 != r2 || c1 != c2) {
printf("\nā Matrix subtraction not possible!\n");
printf("Both matrices must have the same dimensions.\n");
return 1;
}
int A[r1][c1], B[r2][c2], C[r1][c1];
// Read matrices
printf("\nEnter %d elements for first matrix:\n", r1 * c1);
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
printf("A[%d][%d] = ", i, j);
scanf("%d", &A[i][j]);
}
}
printf("\nEnter %d elements for second matrix:\n", r2 * c2);
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
printf("B[%d][%d] = ", i, j);
scanf("%d", &B[i][j]);
}
}
// Subtract matrices
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
// Display result
printf("\n=== Result Matrix ===\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
Sample Output (Error Case):
Enter rows and columns of first matrix: 2 3 Enter rows and columns of second matrix: 2 4 ā Matrix subtraction not possible! Both matrices must have the same dimensions.
Time and Space Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Reading Matrices | O(r Ć c) | O(1) |
| Matrix Subtraction | O(r Ć c) | O(1) |
| Overall | O(r Ć c) | O(r Ć c) |
Matrix Addition vs Subtraction
| Feature | Addition | Subtraction |
|---|---|---|
| Formula | C = A + B | C = A - B |
| Order | Commutative (A+B = B+A) | Not commutative (A-B ā B-A) |
| Zero Matrix | A + 0 = A | A - 0 = A |
| Inverse | A + (-A) = 0 | A - A = 0 |
š» Practice Exercise
Challenge 1: Write a program to subtract two matrices using functions (create a function for matrix subtraction).
Challenge 2: Subtract three matrices: R = A - B - C.
š Click to Show Solution for Challenge 1
#include <stdio.h>
void subtractMatrices(int A[][10], int B[][10], int C[][10], int rows, int cols) {
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
}
void displayMatrix(int matrix[][10], int rows, int cols) {
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows, cols;
printf("Enter rows and columns: ");
scanf("%d %d", &rows, &cols);
int A[10][10], B[10][10], C[10][10];
printf("\nEnter elements of first matrix:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
scanf("%d", &A[i][j]);
}
}
printf("\nEnter elements of second matrix:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
scanf("%d", &B[i][j]);
}
}
subtractMatrices(A, B, C, rows, cols);
printf("\nResult Matrix (A - B):\n");
displayMatrix(C, rows, cols);
return 0;
}
Frequently Asked Questions
1. What is the condition for matrix subtraction?
Both matrices must have the same dimensions (same number of rows and columns). If A is m Ć n, then B must also be m Ć n.
2. How do you subtract two matrices in C?
Use nested loops to iterate through each element and subtract corresponding elements: C[i][j] = A[i][j] - B[i][j].
3. What is the time complexity of matrix subtraction?
The time complexity is O(r Ć c), where r is the number of rows and c is the number of columns.
4. Is matrix subtraction commutative?
No, matrix subtraction is not commutative. A - B is not the same as B - A. The order matters.
5. What is the difference between matrix addition and subtraction?
In addition, we add corresponding elements: C[i][j] = A[i][j] + B[i][j]. In subtraction, we subtract: C[i][j] = A[i][j] - B[i][j].
š” Tip: Always check that the matrices have the same dimensions before performing subtraction. Otherwise, the operation is not defined.