Matrix multiplication is an interesting challenge to solve with arrays and loops. In my classes, we use Java but theoretically this can be applied to any *decent* platform or language.

I dare you to try out some paper matrix multiplication. Getting a feel for it is a great idea. But basically, without doing anything too scary, just remember **Rows** then **Columns**. As my textbook once explained, *RC Cola*.

We assume that our two parameters are *a* and *b*, both arrays of arrays of *integers*, properly formed to be multiplied (that is, each are square with respect to each other).

First, you need figure out how to get the following setup information:

- The first matrix’s number of rows:
`int aRows = a.length`

- The first matrix’s number of columns:
`int aColumns = a[0].length`

- The second matrix’s number of rows:
`int bRows = b.length`

- The second matrix’s number of columns:
`int bColumns = b[0].length`

The result of two matrices that can be multiplied is dependent on the two parent matrices’ rows and columns. In fact, this is shown by `int[][] resultant = int[aRows][bColumns];`

. If you have done some of this by hand already, this should seem fairly standard so far.

The explanation for the following nest of for loops gets cloudy. Initially, it is unintuitive to need *three* loops to perform matrix multiplication, but if you break-down the by-hand procedure, you may find you always perform three distinctive steps.

`for(int i = 0; i < aRows; i++)`

: this loop controls the row position`for(int j = 0; j < bColumns; j++)`

: this loop controls the column position`for(int k = 0; k < aColumns; k++)`

: controls the index of which individual cell is being multiplied and added up

It's a nasty mess. I present to you now, my code, as is.

public static int[][] multiply(int a[][], int b[][]) { int aRows = a.length, aColumns = a[0].length, bRows = b.length, bColumns = b[0].length; if ( aColumns != bRows ) { throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + "."); } int[][] resultant = new int[aRows][bColumns]; for(int i = 0; i < aRows; i++) { // aRow for(int j = 0; j < bColumns; j++) { // bColumn for(int k = 0; k < aColumns; k++) { // aColumn resultant[i][j] += a[i][k] * b[k][j]; } } } return resultant; }

Hopefully, this simple explanation can help someone. Matrix multiplication in *code* is not nearly as elegant as one would think but it is useful nevertheless.

Happy multiplying!

Very helpful.

Thanks

Thanks for this, I appreciate it! Matrices are not fun and my problem was I wasn’t sure if I did need another for loop or two but I had the first 2 loops lol.

Thanks alot Ryan,

I am really glad of the code. I am terrible at manual matrix calculations and always get them wrong.

Thanks a lot. Really cleared up some doubts.

The second matrix’s number of columns: int bRows = b[0].length;

This should be

The second matrix’s number of columns: int bColumns = b[0].length;

Good catch. Thank you!

Hail! Hail!

You saved my day! I was lost in loop that looped in a loop because of the first loop….

:)

The line:

should probably read:

Ah, thanks for catching that.

Nice!!!

If you are multiplying matrices which have square dimensions or matrix a has more columns than rows this code works fine. However, if it has more rows than columns this will not produce the correct result. You might try:

Also, your error checking should include a statement comparing aRows to bColumns thusly:

Just a couple fixes to increase the range of your code.

MergedSorry, I misunderstood matrix multiplication. Your assignment to resultant is correct, however my addendum to your error checking code is still correct.

thank you, this was very helpfull for me :)

The only problem I am having now is how to print the 2D resultant array we get. When I just try to print the resultant array I get a string of letters, numbers, and symbols.

This may work for you, Arrays.deepToString, but it would be just as easy to double foreach loop over the array.

Try this input it will generate result, where as input is not correct, it has to show Exception

int[][] tab1 = {{1,2},{1,2},{1,2}};

int[][] tab2 = {{1,3},{1,4,3,4,5,6}};

You might be interested in my Blogpost: http://martin-thoma.com/matrix-multiplication-python-java-cpp/

That was a great read. Thanks for sharing.