πŸ”’ 10 Powerful Matrix Operations in NumPy (With Output)

 Matrix operations are a core part of data science, machine learning, and scientific computing. Here's a quick walkthrough of 10 essential matrix operations using NumPy in Python, including their outputs.


✅ 1. Multiply Two Matrices

import numpy as np # Multiply two matrices a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) c = np.dot(a, b) print("Multiplication of two matrices:\n", c) # 2nd method using @ operator (Python 3.5+) c = a @ b print("Multiplication of two matrices using @ operator:\n", c)

Output:

Multiplication of two matrices: [[19 22] [43 50]] Multiplication of two matrices using @ operator: [[19 22] [43 50]]

πŸ”„ 2. Transpose of a Matrix

d = np.transpose(a) print("Transpose of matrix a:\n", d)

Output:

Transpose of matrix a: [[1 3] [2 4]]

πŸ” 3. Inverse of a Matrix

try: e = np.linalg.inv(a) print("Inverse of matrix a:\n", e) except np.linalg.LinAlgError: print("Matrix a is singular and cannot be inverted.")

Output:

Inverse of matrix a: [[-2. 1. ] [ 1.5 -0.5]]

πŸ” 4. Check If a Matrix Is Symmetric

def is_symmetric(matrix): return np.array_equal(matrix, matrix.T) symmetric_matrix = np.array([[1, 2, 3], [2, 4, 5], [3, 5, 6]]) if is_symmetric(symmetric_matrix): print("Matrix is symmetric.") else: print("Matrix is not symmetric.")

Output:

Matrix is symmetric.

➕ 5. Sum of Diagonal Elements

def sum_of_diagonal(matrix): return np.trace(matrix) matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("Sum of diagonal elements of the matrix:", sum_of_diagonal(matrix))

Output:

Sum of diagonal elements of the matrix: 15

πŸ”„ 6. Rotate Matrix 90 Degrees Clockwise

def rotate_matrix_90_clockwise(matrix): return np.rot90(matrix, -1) # -1 for clockwise rotation matrix_to_rotate = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) rotated_matrix = rotate_matrix_90_clockwise(matrix_to_rotate) print("Matrix rotated 90 degrees clockwise:\n", rotated_matrix)

Output:

Matrix rotated 90 degrees clockwise: [[7 4 1] [8 5 2] [9 6 3]]

πŸ“ 7. Determinant of a Matrix

def determinant_of_matrix(matrix): return np.linalg.det(matrix) matrix_for_determinant = np.array([[1, 2], [3, 4]]) det = determinant_of_matrix(matrix_for_determinant) print("Determinant of the matrix:\n", det)

Output:

Determinant of the matrix: -2.0000000000000004

πŸŒ€ 8. Print a Matrix in Spiral Order (Placeholder)

def spiral_order(matrix): # Placeholder implementation pass matrix_for_spiral = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("Spiral order of the matrix:\n", spiral_order(matrix_for_spiral))

Output:

Spiral order of the matrix: None

🏝️ 9. Find Number of Islands in a Binary Matrix (Placeholder)

def count_islands(matrix): # Placeholder implementation pass matrix_for_islands = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]]) print("Number of islands in the binary matrix:", count_islands(matrix_for_islands))

Output:

Number of islands in the binary matrix: None

πŸ’° 10. Find the Maximum Sum Submatrix (Placeholder)

def max_sum_submatrix(matrix): # Placeholder implementation pass matrix_for_max_sum = np.array([[1, -2, 3], [-4, 5, -6], [7, 8, -9]]) print("Maximum sum of a submatrix:", max_sum_submatrix(matrix_for_max_sum))

Output:

Maximum sum of a submatrix: None

✅ Final Notes

This walkthrough covered 10 practical matrix operations using NumPy. You can replace the placeholder methods in points 8, 9, and 10 with real algorithms to get functional results.

and logic behind every promblem:-

  1. Multiply two matrices
    πŸ‘‰ np.dot(a, b) or a @ b performs standard matrix multiplication.

  2. Transpose of a matrix
    πŸ‘‰ np.transpose(a) flips the matrix over its diagonal.

  3. Inverse of a matrix
    πŸ‘‰ np.linalg.inv(a) computes the inverse of a non-singular square matrix.

  4. Check if a matrix is symmetric
    πŸ‘‰ np.array_equal(matrix, matrix.T) checks if matrix equals its transpose.

  5. Sum of diagonal elements
    πŸ‘‰ np.trace(matrix) returns the sum of elements on the main diagonal.

  6. Rotate matrix 90° clockwise
    πŸ‘‰ np.rot90(matrix, -1) rotates the matrix 90 degrees clockwise.

  7. Determinant of a matrix
    πŸ‘‰ np.linalg.det(matrix) computes the determinant of a square matrix.

  8. Spiral order of a matrix
    πŸ‘‰ Use a loop to traverse boundaries layer by layer in right→down→left→up order (custom logic needed).

  9. Number of islands in binary matrix
    πŸ‘‰ Use DFS/BFS to count connected groups of 1s (custom logic needed).

  10. Maximum sum submatrix
    πŸ‘‰ Use Kadane’s algorithm in 2D to find the submatrix with the highest sum (custom logic needed).


Comments