CODEBHAVYA โ€ข MATHEMATICS FOR PROGRAMMING

โ–ฆ Matrices for Programming

Learn how rectangular arrays represent data, perform matrix operations step by step, and connect matrices with images, graphs, transformations and machine learning.

๐ŸŽฏ 1. Learning Objectives

After completing this topic, you should be able to:

  • Identify the order, rows, columns and elements of a matrix.
  • Recognize common matrix types and their properties.
  • Perform addition, subtraction, scalar multiplication and matrix multiplication.
  • Find a transpose and the determinant of a 2 ร— 2 matrix.
  • Check whether two matrices are compatible for an operation.
  • Connect matrices with arrays, images, graphs and AI models.

๐Ÿงฉ 2. Matrix Fundamentals

What Is a Matrix?

A matrix is a rectangular arrangement of values in rows and columns.

258147

Order and Element Position

The example has 2 rows and 3 columns, so its order is 2 ร— 3.

Aij means the element in row i, column j. Here A23 = 7.

Programming connection: A matrix maps naturally to a two-dimensional array such as matrix[row][column].

๐Ÿ—‚๏ธ 3. Common Types of Matrices

Row and Column

A row matrix has one row; a column matrix has one column.

Rectangular and Square

A square matrix has equal rows and columns; otherwise it is rectangular.

Zero Matrix

Every element is 0. It acts like zero in matrix addition.

0000

Identity Matrix

Diagonal entries are 1 and all others are 0. AI = IA = A.

1001

Diagonal Matrix

All non-diagonal entries are 0.

4007

Symmetric Matrix

A square matrix is symmetric when AT = A.

2558

โœ… 4. Equality, Addition and Subtraction

Equal matrices must have the same order and equal corresponding elements.
Addition and subtraction require matrices of the same order.
Operate on corresponding positions: Cij = Aij ยฑ Bij.
Matrix addition is commutative: A + B = B + A.
1234
+
5678
=
681012

๐Ÿ”ข 5. Scalar Multiplication

Multiply every element by the scalar value.

3 ร—
1โˆ’240
=
3โˆ’6120

โœ–๏ธ 6. Matrix Multiplication

Multiply each row of the first matrix by each column of the second matrix.

Compatibility rule: If A is m ร— n and B is n ร— p, then AB exists and has order m ร— p. The inner dimensions must match.
1234
ร—
5678
=
19224350
C11 = 1ร—5 + 2ร—7 = 19
C12 = 1ร—6 + 2ร—8 = 22
C21 = 3ร—5 + 4ร—7 = 43
C22 = 3ร—6 + 4ร—8 = 50
Important: Matrix multiplication is generally not commutative. Usually AB โ‰  BA.

๐Ÿ”„ 7. Transpose

The transpose exchanges rows and columns. If A is m ร— n, then AT is n ร— m.

123456
โ†’ AT =
142536
Useful rules: (AT)T = A and (AB)T = BTAT.

๐Ÿ“ 8. Determinant of a 2 ร— 2 Matrix

Formula

abcd

det(A) = ad โˆ’ bc

Example

4325

det(A) = 4ร—5 โˆ’ 3ร—2 = 14.

Singular matrix: If det(A) = 0, the square matrix has no inverse.

๐Ÿ” 9. Inverse of a 2 ร— 2 Matrix

An inverse exists only when ad โˆ’ bc โ‰  0.

Aโˆ’1 = 1/(ad โˆ’ bc)
dโˆ’bโˆ’ca
The inverse reverses a matrix transformation: AAโˆ’1 = Aโˆ’1A = I.

๐Ÿงช 10. Matrix Operation Explorer

Enter two 2 ร— 2 matrices, choose an operation and click Calculate Matrix. Results remain hidden until the button is clicked.

Matrix A

Matrix B

Select an operation or example, then click Calculate Matrix.

๐Ÿ’ป 11. Matrices in Programming

Images

A grayscale image is a matrix of pixel intensities. Colour images use matrices for red, green and blue channels.

Graphs

An adjacency matrix records whether vertices are connected. Undirected graphs produce symmetric adjacency matrices.

Transformations

Rotation, scaling and reflection in computer graphics are expressed using matrix multiplication.

AI and Machine Learning

Datasets, weights and feature transformations are stored and processed as matrices.

๐Ÿงพ 12. Solved Problems

Problem 1: Order and Element

Problem: A has 3 rows and 4 columns. State its order and number of elements.

Solution: Order = 3 ร— 4; elements = 3ร—4 = 12.

Problem 2: Addition

Problem: Add A=[[2,1],[4,3]] and B=[[5,2],[1,6]].

Solution: Add corresponding entries: [[7,3],[5,9]].

Problem 3: Multiplication

Problem: Multiply [[1,2],[0,3]] by [[4,1],[2,5]].

Solution: Row-by-column products give [[8,11],[6,15]].

Problem 4: Determinant

Problem: Find det([[6,2],[3,4]]).

Solution: 6ร—4 โˆ’ 2ร—3 = 24 โˆ’ 6 = 18.

โœ๏ธ 13. Practice Problems

Solve each problem first, then use Show Solution to verify your method.

1. Find the order and number of elements in a matrix with 5 rows and 2 columns.

2. Add [[3,โˆ’1],[2,5]] and [[4,6],[โˆ’2,1]].

3. Can a 2 ร— 3 matrix multiply a 2 ร— 2 matrix in that order?

4. Find the transpose of [[1,4,7],[2,5,8]].

5. Find the determinant of [[7,3],[2,5]] and state whether it has an inverse.

๐Ÿ“ 14. Quick Revision

  • Order = number of rows ร— number of columns.
  • Add or subtract only matrices having the same order.
  • For AB, columns of A must equal rows of B.
  • If A is m ร— n and B is n ร— p, then AB is m ร— p.
  • Transpose exchanges rows and columns.
  • For [[a,b],[c,d]], determinant = ad โˆ’ bc.
  • A 2 ร— 2 matrix has an inverse only when its determinant is non-zero.
  • Images, graph connections and ML data can all be represented by matrices.

โ–ฆ Matrices Practice

Complete 20 questions from easy to hard. Your score, every option, correct answers and explanations appear after final submission.

Start 20-Question Test โ†’