Matrices play a vital role in many diverse fields, including computer graphics, cryptography, and wireless communication. A matrix is a rectangular array of numbers arranged in rows and columns, used to represent a mathematical object or its property.

One of the operations you might need to perform on them is matrix multiplication. This finds uses in many areas such as aerodynamics computations, signal processing, image processing, and seismic analysis. But exactly how do you multiply matrices?

How to Multiply Two Matrices

You represent the order of a matrix as the product of the number of rows (m) and the number of columns (n). To multiply two matrices, the number of columns of the first matrix must be equal to the rows of the second matrix.

If you have two matrices, matrix A of order m × n and B of order n × p, the order of the product matrix will be m × p. For example, suppose you have a matrix A containing two rows (m) and three columns (n) and a matrix B containing three rows (n) and two columns (p). The resultant matrix will consist of two rows and two columns:

Matrix multiplication illustration

You multiply two matrices using the dot product. To get the value of the first element of the resultant matrix, multiply and add the elements of the first row of the first matrix and the first row of the second matrix element by element as:

(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58

Similarly, for the second element, multiply the first row of the first matrix and the second column of the second matrix as:

(1, 2, 3) • (8, 10, 12) = 1×8 + 2×10 + 3×12 = 64

For the third element, multiply the second row of the first matrix and the first column of the second matrix as:

(4, 5, 6) • (7, 9, 11) = 4×7 + 5×9 + 6×11 = 139

For the fourth element, multiply the second row of the first matrix and the second column of the second matrix as:

(4, 5, 6) • (8, 10, 12) = 4×8 + 5×10 + 6×12 = 154

Thus, the resultant matrix is:

Matrix multiplication result illustration

You can explore and build different programs for different operations on matrices such as:

An Algorithm to Multiply Two Matrices

Follow this algorithm to build the program for the multiplication of any two matrices:

  1. Begin the program.
  2. Enter the rows and columns of the first matrix.
  3. Enter the rows and columns of the second matrix.
  4. If the matrices are incompatible for multiplication, print an error and exit.
  5. Define a matrix and enter the numbers in the first matrix.
  6. Define another matrix and enter the number in the second matrix.
  7. Define a matrix to store the result of the multiplication of the two matrices.
  8. Set a loop to iterate over the row of the first matrix.
  9. Set up an inner loop to iterate over the column of the second matrix.
  10. Set another inner loop to iterate over the column of the first matrix.
  11. Multiply and add the elements using the formula mul[i][j] += m1[i][k] * m2[k][j] and store the result of the multiplication in the resultant matrix.
  12. Display the resultant matrix.
  13. Exit the program.

How to Perform Matrix Multiplication Using C

The entire source code for matrix multiplication using C is present in this GitHub repository and is free to use.

Import the stdio library to input numbers and display the output accordingly. Declare the main function and ask the user to enter the number of columns and number of rows for both matrices using the print() function.

Use the scanf() function to receive input. %d is the decimal format specifier that ensures that the program reads input as a number.

        #include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int r1, r2, c1, c2;

    printf("Enter the number of rows for the first matrix:\n");
    scanf("%d", &r1);
 
    printf("Enter the number of columns for the first matrix:\n");
    scanf("%d", &c1);
 
    printf("Enter the number of rows for the second matrix:\n");
    scanf("%d", &r2);
 
    printf("Enter the number of columns for the second matrix:\n");
    scanf("%d", &c2);

Check that matrix multiplication is possible. If the number of columns of the first matrix is not equal to the number of rows of the second matrix, display an error and exit.

            if (c1 != r2) {
        printf("The matrices cannot be multiplied together");
       exit(-1);
    }

If all is good, define two multidimensional arrays, m1 and m2, with the size that the user provided. Ask the user to enter the elements of both matrices one by one. Use a nested for loop to take the input for both the row and column of the matrix. The outer for loop iterates over the rows of the matrix and the inner loop over the column of the matrix.

            int m1[r1][c1], m2[r2][c2];
    printf("Enter the elements of the first matrix\n");
 
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c1; j++) {
            scanf("%d", &m1[i][j]);
        }
    }
 
    printf("Enter the elements of the second matrix\n");
 
    for (int i = 0; i < r2; i++) {
        for (int j = 0; j < c2; j++) {
            scanf("%d",&m2[i][j]);
        }
    }

Define a third matrix, mul, of order r1 * c2 to store the result. Use a nested for loop to perform the multiplication. The outermost for loop iterates over the rows, the next inner loop iterates over the columns, and the innermost performs the multiplication. Use the formula mul[i][j] += m1[i][k] * m2[k][j] to multiply the elements of the matrix.

The formula uses the shorthand operator += to add mul[i][j] to the computed expression and store it. Remember to initialize the result to zero before adding to it.

            int mul[r1][c2];
 
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            mul[i][j] = 0;
 
            for (int k = 0; k < c1; k++) {
                mul[i][j] += m1[i][k] * m2[k][j];
            }
        }
    }

Display the multiplied matrix using a nested for loop that iterates over the resultant matrices' rows and columns. Use the new-line character (\n) to display each of the rows on a separate line. Return 0 to exit the main function and the program.

            printf("The multiplied matrix is: \n");     
 
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            printf("%d\t", mul[i][j]);
        }
 
        printf("\n");
    }
 
    return 0;
}

The Output of the Matrix Multiplication Program

You should see something like the following output when you run the matrix multiplication program:

Output of Matrix Multiplication

If you enter invalid input, the matrix multiplication fails and you’ll see something like this:

Output of Matrix Multiplication when not possible

Matrices Have Many Uses

Various fields use matrices like science, commerce, economics, geology, robotics, and animation. You'll mainly use matrices in mathematics to solve linear equations and represent transformations such as rotation or translation. Matrices can calculate the amount of reflection and refraction as well as solve AC network equations in electrical circuits.

Apart from educational applications, you can use matrices for the analysis of survey data, voting data, compute item lists, and other data sets.