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

Understanding the Row × Column Rule

If there is one concept that you should remember from this entire article, it is this:

Each element of the result matrix is obtained by multiplying one row of the first matrix with one column of the second matrix.

This is known as the Row × Column Rule.

Let’s understand why this rule works.

Suppose we have the following matrices.

Matrix A (2 × 3)

1   2   3
4   5   6

Matrix B (3 × 2)

7    8
9   10
11  12

The result matrix will have a size of 2 × 2.

Now let’s calculate each position individually.


Position (0,0)

Take:

  • First row of Matrix A
  • First column of Matrix B
Row :    1   2   3

Column:  7
         9
        11

Multiply corresponding elements.

1 × 7

2 × 9

3 × 11

Now add them.

7 + 18 + 33

= 58

Therefore,

Result[0][0] = 58

Position (0,1)

Take

  • First row
  • Second column
1   2   3

8
10
12

Multiply.

1×8

2×10

3×12

Add.

8 + 20 + 36

= 64

Therefore,

Result[0][1] = 64

Position (1,0)

Take

Second row

4   5   6

First column

7
9
11

Multiply.

4×7

5×9

6×11

Add.

28 + 45 + 66

= 139

Position (1,1)

Take

Second row

4   5   6

Second column

8
10
12

Multiply.

4×8

5×10

6×12

Add.

32 + 50 + 72

= 154

The final matrix becomes

58   64

139 154

Notice something very important.

Every answer comes from

One row of the first matrix and one column of the second matrix.

That is why matrix multiplication is always called the Row × Column method.


Why Does the Rule Always Work?

Suppose the matrices are

A = m × n

B = n × p

The first matrix has n columns.

The second matrix has n rows.

Since these two dimensions are equal, every element in a row has exactly one matching element in the corresponding column.

For example,

1   2   3

matches with

7
9
11

because both contain three elements.

Imagine trying to multiply

1   2

with

7
9
11

The row contains only two elements, while the column contains three.

There is no third element in the row to match with 11.

This is exactly why multiplication would not be possible.

The matching dimensions guarantee that every element has a corresponding partner.


Understanding the Common Dimension

Consider

2 × 3

×

3 × 4

The number 3 appears in both matrices.

This number is called the common dimension.

It represents:

  • Columns of the first matrix
  • Rows of the second matrix

Every multiplication happens across this common dimension.

For this reason, in Java we repeatedly iterate through this dimension while calculating each element of the result matrix.


Visual Representation

Suppose we want to calculate the highlighted element.

Matrix A

      1   2   3
      ---------
Row → 1   2   3
      4   5   6


Matrix B

       ↓
       8
      10
      12

Take

  • the selected row
  • the selected column

Multiply corresponding elements.

1×8

2×10

3×12

Add everything.

The answer becomes the highlighted position in the result matrix.

Every element follows exactly the same process.


A Simple Memory Trick

Remember this sentence forever.

Row from the First Matrix × Column from the Second Matrix

If you remember this single sentence, matrix multiplication becomes very easy.

Another useful formula is

(m × n)

×

(n × p)

=

(m × p)

Think of the middle dimensions as two puzzle pieces.

If they fit together, multiplication is possible.

If they do not fit, multiplication cannot be performed.


Common Mistakes Made by Beginners

Mistake 1

Thinking that both matrices must have the same size.

For example,

2 × 2

×

2 × 2

Students often believe this is the only valid case.

This is incorrect.

The following multiplication is also perfectly valid.

2 × 3

×

3 × 5

Mistake 2

Checking whether the number of rows are equal.

Wrong check

Rows of A = Rows of B

Correct check

Columns of A = Rows of B

Always compare the columns of the first matrix with the rows of the second matrix.


Mistake 3

Thinking that

A × B

=

B × A

This is false.

For example,

A = 2 × 3

B = 3 × 2

Then

A × B

=

2 × 2

But

B × A

=

3 × 3

The sizes are different.

Even when both products are possible, the answers are usually different.

Therefore,

Matrix multiplication is not commutative.


Mistake 4

Using the wrong dimensions for the result matrix.

Many beginners think

2 × 3

×

3 × 4

=

3 × 3

This is incorrect.

The correct result size is

Rows of First Matrix

×

Columns of Second Matrix

=

2 × 4

Practical Applications of Matrix Multiplication

Matrix multiplication is not just a mathematical exercise.

It is widely used in real-world software systems.

Some important applications include:

  • Computer Graphics
  • 2D and 3D Transformations
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Image Processing
  • Scientific Simulations
  • Cryptography
  • Data Analysis
  • Neural Networks
  • Game Development
  • Recommendation Systems

Almost every AI model performs millions of matrix multiplications every second.


Quick Revision

Remember these five rules.

Rule 1

A matrix is written as

Rows × Columns

Rule 2

Matrix multiplication is possible only when

Columns of First Matrix

=

Rows of Second Matrix

Rule 3

The result matrix size is

Rows of First Matrix

×

Columns of Second Matrix

Rule 4

Every element of the result matrix is obtained by

Row × Column


Rule 5

Matrix multiplication is not commutative.

A × B

≠

B × A

Frequently Asked Questions (FAQs)

1. Can two matrices of different sizes be multiplied?

Yes, provided the number of columns in the first matrix equals the number of rows in the second matrix.


2. Why must the inner dimensions be equal?

Because every element of a row must have a matching element in the corresponding column.


3. Can two 3 × 3 matrices be multiplied?

Yes.

The result will also be a 3 × 3 matrix.


4. Can a 3 × 2 matrix be multiplied with a 2 × 4 matrix?

Yes.

The result will be a 3 × 4 matrix.


5. Can a 3 × 2 matrix be multiplied with a 3 × 3 matrix?

No.

The number of columns of the first matrix (2) is not equal to the number of rows of the second matrix (3).


Conclusion

Matrix multiplication becomes much easier once you understand that every element of the result matrix is produced by multiplying one row from the first matrix with one column from the second matrix.

The most important rule to remember is:

  • The columns of the first matrix must equal the rows of the second matrix.
  • The result matrix will always have the number of rows of the first matrix and the number of columns of the second matrix.
  • Every element is calculated using the Row × Column method.

Once these concepts are clear, writing the Java program is straightforward.

In Part 2, we will build a complete Java program for matrix multiplication that:

  • Accepts user input.
  • Checks whether multiplication is possible.
  • Performs the multiplication using three nested loops.
  • Displays the result matrix.
  • Explains every line of code in detail.

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 *