Skip to main content

Help with Matrix

1 reply [Last post]
Joined: 2006-02-17

a java application program to multiply two matrices A and
B and output the product matrix C. Matrix A must have the same number of columns as matrix B has rows. The maximum matrix sizes are 5

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-07-05

Try this code, pastit as private in your clase or as a static method in other one.

private int[][] matrixMult(int[][] m1, int[][] m2)
int[][] m3 = new int[m1.length][m2[0].length];
for(int i=0;i {
for(int j=0;j {
for(int k=0;k {
m3[i][j] += m1[i][k]*m2[k][j];
return m3;
return null;

the null happens when the sizes of the matrix are not compatible.
n m n m

as you can see if m in a has the same size of n in b.
the calculation can proceed.

n in a and m in b define the new dimensions of the future matrix.