Matrix: A matrix(singular of matrices) in mathematical terms is simply an arrangement of numbers in a definite number of rows and columns.
Few examples of a matrix are as follows:
Matrix Dimensions: The dimensions of a matrix also known as the Order of a matrix gives the exact size of the matrix i.e. the number of elements a matrix can store. The dimensions of a matrix are represented by (N x M); where N and M are the number of rows and the number of columns of the matrix respectively.
Hence, the size of a matrix is equal to the product of N (number of rows) and M (number of columns).
The most common and basic mathematical operations on matrices are as follows:
- Taking Inputs for the matrix elements and storing them in the matrix.
- Taking transpose of a matrix and storing it in a new matrix.
- Addition of two matrices and storing the sum in a new matrix.
- Subtraction of two matrices and storing the difference in a new matrix.
- Multiplication of two matrices and storing the product in a new matrix.
- Calculating Trace of a matrix i.e the sum of the principal diagonal elements of a square matrix [a square matrix is a matrix for which N (number of rows) = M (number of columns)].
Now Let’s dive into the actual coding/programming in C++ to perform these mathematical operations on matrices.
Taking Input for a Matrix
#include <iostream>
using namespace std;
int main()
{
int N, M;
// Taking input for the order of a matrix
cout<<"Enter the order of the matrix:"<<endl;
cin>>N>>M;
// Defining a matrix variable
int A[N][M];
// Taking input for the elements of the matrix in the matrix form
cout<<"Enter Matrix elements:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cin>>A[i][j];
}
// Printing the input matrix
cout<<"Input Matrix:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cout<<A[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output:
Enter the order of the matrix:
3 3
Enter Matrix elements:
4 5 6
3 5 7
7 9 1
Input Matrix:
4 5 6
3 5 7
7 9 1
Also Read Types of Looping statements in C/C++
Transpose of a Matrix (AT)
Transpose of a matrix is defined as the matrix obtained by interchanging the rows of the matrix with the columns of that matrix.
For finding the transpose of a matrix, that matrix need not to be a square matrix.
#include <iostream>
using namespace std;
int main()
{
int N, M;
// Taking input for the order of matrix A
cout<<"Enter the order of the matrix:"<<endl;
cin>>N>>M;
// Defining a matrix variables
int A[N][M], T[M][N];
// Taking input for the elements of the matrix
cout<<"Enter Matrix elements:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cin>>A[i][j];
}
// Calculating transpose of matrix A
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
T[j][i] = A[i][j];
}
// Printing the transpose of the matrix
cout<<"Transpose of Matrix:"<<endl;
for(int i = 0; i < M; i++)
{
for(int j = 0; j < N; j++)
cout<<T[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output:
Enter the order of the matrix:
3 4
Enter Matrix elements:
5 6 7 1
5 7 2 3
6 7 4 3
Transpose of Matrix:
5 5 6
6 7 7
7 2 4
1 3 3
Also Read Five Ways to Calculate Power in C++
Adding Two Matrices (A + B)
To perform the addition over two matrices, the two matrices should have the same number of rows and columns.
To add matrix A with matrix B, we have to take the element-wise addition of the corresponding elements of the two matrices.
#include <iostream>
using namespace std;
int main()
{
int N, M;
// Taking input for the order of matrices
cout<<"Enter the order of matrices:"<<endl;
cin>>N>>M;
// Defining matrix variables
int A[N][M], B[N][M], C[N][M];
// Taking input for the elements of matrix A
cout<<"Enter elements of Matrix A:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cin>>A[i][j];
}
// Taking input for the elements of matrix B
cout<<"Enter elements of Matrix B:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cin>>B[i][j];
}
// Calculating the sum of matrices
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
C[i][j] = A[i][j] + B[i][j];
}
// Printing the sum of matrices A and B
cout<<"Sum of Matrix A and B:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cout<<C[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output:
Enter the order of matrices:
3 3
Enter elements of Matrix A:
2 3 4
5 6 7
2 3 4
Enter elements of Matrix B:
6 7 8
3 6 4
3 4 2
Sum of Matrix A and B:
8 10 12
8 12 11
5 7 6
Subtracting Two Matrices (A – B)
To perform the subtraction over two matrices, the two matrices should have the same number of rows and columns.
To subtract matrix B from matrix A, we have to take the element-wise difference of the corresponding elements of the two matrices.
#include <iostream>
using namespace std;
int main()
{
int N, M;
// Taking input for the order of matrices
cout<<"Enter the order of matrices:"<<endl;
cin>>N>>M;
// Defining matrix variables
int A[N][M], B[N][M], C[N][M];
// Taking input for the elements of matrix A
cout<<"Enter elements of Matrix A:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cin>>A[i][j];
}
// Taking input for the elements of matrix B
cout<<"Enter elements of Matrix B:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cin>>B[i][j];
}
// Calculating the difference of matrices
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
C[i][j] = A[i][j] - B[i][j];
}
// Printing the difference of matrix A and B
cout<<"Difference of Matrix A and B:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cout<<C[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output:
Enter the order of matrices:
3 5
Enter elements of Matrix A:
1 2 3 4 5
4 6 7 8 8
2 3 5 3 6
Enter elements of Matrix B:
5 6 2 4 5
7 8 4 2 1
6 8 4 4 2
Difference of Matrix A and B:
-4 -4 1 0 0
-3 -2 3 6 7
-4 -5 1-1 4
Multiplication of Two Matrices (A x B)
We can multiply two matrices only when the number of columns in matrix A is equal to the number of rows in matrix B i.e. If A is of order (N x M) and B is of order (P x Q) then product A x B is possible only when (M = P) and the order of the product matrix will be (N x Q).
#include <iostream>
using namespace std;
int main()
{
int N, M, P, Q;
// Taking input for the order of matrices
cout<<"Enter the order of matrix A:"<<endl;
cin>>N>>M;
cout<<"Enter the order of matrix B:"<<endl;
cin>>P>>Q;
// Defining matrix variables
int A[N][M], B[P][Q], C[N][Q];
// Taking input for the elements of matrix A and B
cout<<"Enter elements of Matrix A:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cin>>A[i][j];
}
cout<<"Enter elements of Matrix B:"<<endl;
for(int i = 0; i < P; i++)
{
for(int j = 0; j < Q; j++)
cin>>B[i][j];
}
// Calculating the product of matrices
for(int i = 0; i < N; i++)
{
for(int j = 0; j < Q; j++)
{
C[i][j] = 0;
for(int k = 0; k < M; k++ )
C[i][j] = C[i][j] + (A[i][k] * B[k][j]);
}
}
// Printing the product of matrix A and B
cout<<"Product of Matrix A and B:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < Q; j++)
cout<<C[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output:
Enter the order of matrix A:
2 3
Enter the order of matrix B:
3 3
Enter elements of Matrix A:
1 1 4
1 2 3
Enter elements of Matrix B:
1 2 3
4 5 6
7 8 9
Product of Matrix A and B:
33 39 45
30 36 42
Trace of a Matrix (tr(A))
Trace of a square matrix is defined as the sum of the principal diagonal elements of that matrix.
For finding the trace of a matrix, that matrix needs to be a square matrix.
#include <iostream>
using namespace std;
int main()
{
int N, M;
// Taking input for the order of matrix A
cout<<"Enter the order of the matrix:"<<endl;
cin>>N>>M;
// Defining a matrix variable
int A[N][M], trace = 0;
// Taking input for the elements of the matrix
cout<<"Enter Matrix elements:"<<endl;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cin>>A[i][j];
}
// Calculating trace of matrix A
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
if(i == j)
trace = trace + A[i][j];
}
// Printing the transpose of the matrix
cout<<"Trace of Matrix A = "<<trace<<endl;
return 0;
}
Output:
Enter the order of the matrix:
3 3
Enter Matrix elements:
2 3 5
6 7 4
7 4 9
Trace of Matrix A = 18