Matrix Multiplication in Java – A Complete Beginner’s Guide (Part 1)

Introduction

Matrices are one of the most important concepts in mathematics and computer science. They are widely used in Data Structures and Algorithms (DSA), Computer Graphics, Artificial Intelligence (AI), Machine Learning, Image Processing, Game Development, Robotics, and Scientific Computing.

Although matrix multiplication may look complicated at first, it becomes very easy once you understand one simple principle:

Take one row from the first matrix and one column from the second matrix. Multiply the corresponding elements and add the results.

In this article, you will learn everything you need to know about matrix multiplication before writing a single line of Java code.

By the end of this tutorial, you will understand:

  • What a matrix is
  • What matrix multiplication means
  • Conditions for matrix multiplication
  • How to determine the size of the result matrix
  • How to perform matrix multiplication manually
  • Why the “Row × Column” rule always works
  • Common mistakes beginners make

In Part 2, we will implement the complete Matrix Multiplication program in Java with proper validation and user input.


What is a Matrix?

A matrix is a rectangular arrangement of numbers organized into rows and columns.

Example:

1   2   3
4   5   6

This matrix contains:

  • 2 Rows
  • 3 Columns

Therefore, its size is written as:

2 × 3

Another example:

7   8
9   10
11  12

This matrix contains:

  • 3 Rows
  • 2 Columns

Its size is:

3 × 2

Whenever we write a matrix size, we always write:

Rows × Columns

What is Matrix Multiplication?

Matrix multiplication is a mathematical operation in which two matrices are multiplied to produce a new matrix.

Unlike ordinary number multiplication, matrix multiplication follows a specific rule.

For example,

Suppose we have two matrices.

Matrix A

1   2   3
4   5   6

Size:

2 × 3

Matrix B

7    8
9   10
11  12

Size:

3 × 2

These two matrices can be multiplied because they satisfy a very important condition.


Condition for Matrix Multiplication

Two matrices can be multiplied only if:

Number of columns of the first matrix = Number of rows of the second matrix

This is the most important rule.

For example,

First Matrix

2 × 3

Second Matrix

3 × 2

Compare:

Columns of First Matrix = 3

Rows of Second Matrix = 3

Since

3 = 3

Matrix multiplication is possible.


Example 1

2 × 3

×

3 × 5

Columns of first matrix = 3

Rows of second matrix = 3

Therefore,

✅ Multiplication is possible.


Example 2

3 × 2

×

2 × 4

Columns of first matrix = 2

Rows of second matrix = 2

Therefore,

✅ Multiplication is possible.


Example 3

3 × 2

×

3 × 3

Columns of first matrix = 2

Rows of second matrix = 3

Since

2 ≠ 3

❌ Matrix multiplication is NOT possible.


Example 4

2 × 4

×

3 × 2

Columns of first matrix = 4

Rows of second matrix = 3

Since

4 ≠ 3

❌ Matrix multiplication is NOT possible.


Memory Trick

Always remember this formula:

(m × n)

×

(n × p)

=

(m × p)

Notice carefully.

The middle dimensions

n

and

n

must always be equal.

The outer dimensions become the size of the answer.


How to Find the Size of the Result Matrix

Suppose

A = 2 × 3

B = 3 × 2

The result matrix will have

Rows of First Matrix

×

Columns of Second Matrix

Therefore,

2 × 2

Another example

3 × 2

×

2 × 4

Result

3 × 4

One more example

5 × 6

×

6 × 8

Result

5 × 8

Notice something interesting.

The middle numbers disappear.

Only the outside numbers remain.


Quick Practice

Can you determine whether these matrices can be multiplied?

Matrix AMatrix BPossible?Result Size
2 × 33 × 4✅ Yes2 × 4
3 × 22 × 5✅ Yes3 × 5
4 × 44 × 2✅ Yes4 × 2
2 × 54 × 3❌ No
5 × 23 × 5❌ No

Practice identifying the dimensions before attempting multiplication.


How Does Matrix Multiplication Actually Work?

This is where most students become confused.

Fortunately, the process is very simple.

Suppose we have

Matrix A

1   2   3
4   5   6

Matrix B

7    8
9   10
11  12

We already know that

2 × 3

×

3 × 2

=

2 × 2

So the result matrix will contain four elements.

?   ?

?   ?

Each question mark is calculated separately.


Calculating the First Element

Take

First Row of Matrix A

1   2   3

First Column of Matrix B

7
9
11

Multiply corresponding elements.

1 × 7 = 7

2 × 9 = 18

3 × 11 = 33

Now add them.

7 + 18 + 33 = 58

Therefore,

58

becomes the first element of the result matrix.


Calculating the Second Element

Take

First Row of Matrix A

1   2   3

Second Column of Matrix B

8
10
12

Multiply.

1 × 8 = 8

2 × 10 = 20

3 × 12 = 36

Add.

8 + 20 + 36 = 64

Second element becomes

64

Calculating the Third Element

Take

Second Row of Matrix A

4   5   6

First Column of Matrix B

7
9
11

Multiply.

4 × 7 = 28

5 × 9 = 45

6 × 11 = 66

Add.

28 + 45 + 66 = 139

Third element is

139

Calculating the Fourth Element

Take

Second Row

4   5   6

Second Column

8
10
12

Multiply.

4 × 8 = 32

5 × 10 = 50

6 × 12 = 72

Add.

32 + 50 + 72 = 154

The final element becomes

154

Final Result Matrix

The completed matrix is

58   64

139 154

At this point, you already know how to perform matrix multiplication manually. The only thing left is understanding why the Row × Column rule works and how to convert this manual process into a Java program.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *