Skip to main content

Help with Matrix

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

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
amigosdemaurilio
Offline
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)
{
if(m1[0].length==m2.length)
{
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
a[2][3]*b[3][1]

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.