Multiplying Matrices in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Division in Java: Code & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Matrices
  • 0:25 Multiplying Matrices
  • 1:33 Multiplying Matrices in Java
  • 3:48 Invalid Matrices
  • 4:22 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Multiplying matrices can be a daunting task in mathematics. This lesson will explain matrix multiplication, and show you how to use Java to multiply matrices.


A matrix is a table of numbers arranged in rows and columns. Think of creating a table in Microsoft Word. You can create any combination of rows and columns. A matrix is described in terms of the number of rows times the number of columns. For example, 3 x 3 is a matrix with three rows and three columns.

Basic matrix

Multiplying Matrices

When multiplying matrices, there are a couple things to remember. Matrix multiplication is not commutative. In order to be able to multiply matrices, they have to meet one requirement. The number of columns in the first matrix has to equal the number of rows in the second matrix. The size of the resulting matrix will be the determined by the number of rows in the first matrix and the number of columns in the second matrix. In this example, the initial matrix has 3 columns and the second matrix has 3 rows so they can be multiplied. The resulting matrix will be 3 x 3.

matrix multiplication dot method

To multiply matrices, we find the dot product. We multiply each of the terms in the first row (3, 5, 7) by the corresponding terms in the first column (1, 7, 13) of the second matrix and then add those numbers together. It looks like this:

( 3 * 1 ) + (5 * 7) + (7 * 13) = 129

We continue to do this for each term in the resulting matrix. Now, we could do all of this by hand, but we have a powerful tool at our disposal, Java. With a little coding effort, we can write a routine that will multiply our matrices.

Multiplying Matrices in Java

First, let's review how Java will see our matrices. In Java, we create two-dimensional arrays (rows/columns) to hold our data. The rows and columns will be referenced as follows. Remember that Java starts counting at zero!

Java matrix example

We can now define the matrix we covered earlier in our code. In order to create a matrix, we use a 2-dimensional array, int [ ] [ ] myMatrix. To pre-fill the array, the entire array is denoted with curly brackets. Each row has its own set of curly brackets.

int[][] myMatrixA = {
int[][] myMatrixB = {

Now comes the tricky part. We will have THREE nested loops that walk across the rows of the first matrix, then down the columns of the second matrix. These loops will take care of the math, but we have to set them up right.

First, let's take a moment to set up some counters for our loops. We then create variables to count the rows and columns for each matrix. We'll need these to know when to stop processing (we can't go past the end of the array or there will be nasty error messages). Finally, we process the array multiplication. The code follows the same mathematical logic we described above.

int i, j, k, m, n;
//rows and columns for each matrix
int rowsA = myMatrixA.length;
int colsA = myMatrixA[0].length;
int rowsB = myMatrixB.length;
int colsB = myMatrixB[0].length;
//new matrix to hold result
int[][] myMatrixC = new int [rowsA][colsB];
//start across rows of A
for (i = 0; i < rowsA; i++) {
  //work across cols of B
  for(j = 0; j < colsB; j++) {
   //now complete the addition and multiplication
   for(k = 0; k < colsA; k++) {
    myMatrixC[i][j] += myMatrix [i][k] * myMatrixB[k][j];

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account