A table of marks with one row for each student and one column for each subject is a matrix. Matrices are used wherever data has two dimensions: in timetables, images, maps and the solution of equations.
Matrices and Two-Dimensional Arrays
Definition: Matrix. A rectangular arrangement of numbers in rows and columns.
A matrix with m rows and n columns is said to be of order m × n. In C it is stored in a two-dimensional array. The element in row i and column j of matrix A is a[i][j], and both indices start at 0.
Matrix Addition and Subtraction
Two matrices can be added or subtracted only when they have the same order. The result has that order too. Each element of the result is obtained from the elements in the same position of the two matrices:
- addition:
c[i][j] = a[i][j] + b[i][j] - subtraction:
c[i][j] = a[i][j] - b[i][j]
For A with rows (1, 2) and (3, 4) and B with rows (5, 6) and (7, 8):
- A + B has rows (6, 8) and (10, 12);
- A − B has rows (−4, −4) and (−4, −4).
Both operations are coded with two nested loops that visit every position.
Transpose of a Matrix
Definition: Transpose. The matrix obtained by interchanging the rows and columns of a given matrix.
The first row of the matrix becomes the first column of its transpose, the second row becomes the second column, and so on. If A is of order m × n, its transpose is of order n × m. The rule is:
t[j][i] = a[i][j]
Table 4.9 summarizes the three operations.
Table 4.9: Summary of matrix operations
| Operation | Condition | Rule | Order of result |
|---|---|---|---|
| Addition | Same order | c[i][j] = a[i][j] + b[i][j] | m × n |
| Subtraction | Same order | c[i][j] = a[i][j] − b[i][j] | m × n |
| Transpose | None | t[j][i] = a[i][j] | n × m |
Points of Care in Coding
- The orders of the two matrices must be compared before addition or subtraction; if they differ, the operation is refused.
- The result should be stored in a separate array, so that the original matrices are preserved.
- For the transpose of a matrix that is not square, the result array must have n rows and m columns.
- Subtraction is not commutative: A − B is not the same as B − A.
Example 4.9: Addition and transpose
Problem: For A with rows (1, 2) and (3, 4) and B with rows (5, 6) and (7, 8), display A + B and the transpose of A.
Solution:
- Add the elements in corresponding positions with nested loops.
- Form the transpose with
t[j][i] = a[i][j]. - Display each result row by row.
#include <stdio.h>
int main(void)
{
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int c[2][2], t[2][2], i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
c[i][j] = a[i][j] + b[i][j];
t[j][i] = a[i][j];
}
}
printf("A + B:\n");
for (i = 0; i < 2; i++)
{
printf("%3d %3d\n", c[i][0], c[i][1]);
}
printf("Transpose of A:\n");
for (i = 0; i < 2; i++)
{
printf("%3d %3d\n", t[i][0], t[i][1]);
}
return 0;
}
Output:
A + B:
6 8
10 12
Transpose of A:
1 3
2 4
Result: The sum has rows (6, 8) and (10, 12). In the transpose, the first row of A, (1, 2), has become the first column.
Common mistakes
- Adding or subtracting matrices of different orders.
- Writing
t[i][j] = a[i][j]for the transpose, which merely copies the matrix. - Storing the transpose in the same array while computing it, which overwrites elements still needed.
Key takeaways
- A matrix is stored in a two-dimensional array and processed with nested loops.
- Addition and subtraction need matrices of the same order and work position by position.
- The transpose interchanges rows and columns:
t[j][i] = a[i][j]. - The transpose of an m × n matrix is of order n × m.
Practice questions
- Can a 2 × 3 matrix be added to a 3 × 2 matrix?
- What is the order of the transpose of a 4 × 2 matrix?
- If A has rows (2, 5) and (1, 3), write its transpose.
Answers to the practice questions
- No. The two matrices must have the same order.
- 2 × 4.
- Rows (2, 1) and (5, 3).
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV
