Matrix Multiplication and Normalization in C program

In C programming, matrix multiplication and normalization are fundamental operations in linear algebra. Matrix multiplication follows specific rules where the number of columns in the first matrix must equal the number of rows in the second matrix. Matrix normalization scales each row so that its Euclidean norm equals 1.

Syntax

// Matrix Multiplication
for (i = 0; i < rows1; i++) {
    for (j = 0; j < cols2; j++) {
        for (k = 0; k < cols1; k++) {
            result[i][j] += matrix1[i][k] * matrix2[k][j];
        }
    }
}

// Matrix Normalization  
for (i = 0; i < rows; i++) {
    norm_factor = 1.0 / sqrt(sum_of_squares);
    for (j = 0; j < cols; j++) {
        normalized[i][j] = norm_factor * original[i][j];
    }
}

Matrix Multiplication Rules

For matrices P (a × b) and Q (c × d), multiplication P × Q is possible only if b = c. The resultant matrix R will have dimensions (a × d).

P (a×b) Q (b×d) × = R (a×d) Condition: columns of P = rows of Q

Algorithm for Matrix Multiplication

matrixMultiply(P, Q):
Begin
  if columns_of_P != rows_of_Q, then exit
  initialize result matrix R with zeros
  for i = 0 to rows_of_P - 1:
    for j = 0 to columns_of_Q - 1:
      for k = 0 to columns_of_P - 1:
        R[i][j] += P[i][k] * Q[k][j]
End

Matrix Normalization

Matrix normalization scales each row so that its magnitude (Euclidean norm) becomes 1. For a row with elements [a?, a?, ..., a?], the normalization factor is 1/?(a?² + a?² + ... + a?²).

Example: Matrix Multiplication

#include <stdio.h>

int main() {
    int matrix1[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int matrix2[3][2] = {{7, 8}, {9, 10}, {11, 12}};
    int result[2][2] = {0};
    
    printf("Matrix 1 (2x3):<br>");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix1[i][j]);
        }
        printf("<br>");
    }
    
    printf("\nMatrix 2 (3x2):<br>");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", matrix2[i][j]);
        }
        printf("<br>");
    }
    
    // Matrix multiplication
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 3; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
    
    printf("\nResultant Matrix (2x2):<br>");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", result[i][j]);
        }
        printf("<br>");
    }
    
    return 0;
}
Matrix 1 (2x3):
1 2 3 
4 5 6 

Matrix 2 (3x2):
7 8 
9 10 
11 12 

Resultant Matrix (2x2):
58 64 
139 154 

Example: Matrix Normalization

#include <stdio.h>
#include <math.h>

int main() {
    int matrix[2][3] = {{4, 5, 6}, {1, 2, 3}};
    double normalized[2][3];
    int rows = 2, cols = 3;
    
    printf("Original Matrix:<br>");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("<br>");
    }
    
    // Normalize each row
    for (int i = 0; i < rows; i++) {
        double sum_of_squares = 0.0;
        
        // Calculate sum of squares for row i
        for (int j = 0; j < cols; j++) {
            sum_of_squares += matrix[i][j] * matrix[i][j];
        }
        
        double norm_factor = 1.0 / sqrt(sum_of_squares);
        
        // Normalize the row
        for (int j = 0; j < cols; j++) {
            normalized[i][j] = norm_factor * matrix[i][j];
        }
        
        printf("Sum of squares for row %d: %.0f<br>", i, sum_of_squares);
    }
    
    printf("\nNormalized Matrix:<br>");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%.3f ", normalized[i][j]);
        }
        printf("<br>");
    }
    
    return 0;
}
Original Matrix:
4 5 6 
1 2 3 

Sum of squares for row 0: 77
Sum of squares for row 1: 14

Normalized Matrix:
0.456 0.570 0.684 
0.267 0.535 0.802 

Key Points

  • Matrix multiplication requires the inner dimensions to match − columns of first matrix must equal rows of second matrix.
  • Matrix normalization ensures each row has unit magnitude, useful in machine learning and data preprocessing.
  • Time complexity of matrix multiplication is O(n³) for square matrices.
  • Always check matrix dimensions before performing operations to avoid runtime errors.

Conclusion

Matrix multiplication and normalization are essential operations in C programming for mathematical computations. Understanding these concepts is crucial for linear algebra applications and numerical computing.

Updated on: 2026-03-15T12:32:56+05:30

937 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements