I am learning java through a book. After finishing each chapter, i work out problems given at end of each chapter . when i tried to solve a problem , i could not frame the logic exactly. Please help to solve following exercise :
Write a program to find the product of two matrices A(3,4) and B(4,2).

I tried in code as below:

``````/* Exercise 5.14 Write a program to find the product of two matrices A(3,4) and
B(4,2) page : 71
*/
class Matrix	{
public static void main(String args[]) {
int i,j,k;
int r[][] = new int[3][2]; // resultant array is used  to store product
int a[][] = { //A(3,4)
{1,2,3,4},
{5,6,7,8},
{9,1,2,3}
};
int b[][] ={  // B(4,2)
{1,2},
{3,4},
{5,6},
{7,8}
};
// i dont know how to handle loops in this case.
for(i=0;i<2;i++)	{
for(j=0;j<3;j++)	{
for(k=0;k<4;i++)	{
r[j][i] += ( a[j][k] * b[k][i]);
}
}
}
// finally displaying the product matrix.
for(i=0;i<3;i++)	{
for(j=0;j<2;j++)	{
System.out.println(r[i][j]+"\t");
}
System.out.print("\n");
}
}
}``````

Its easy when two matrices(A(3,3) and B(3,3)) have same number of rows and columns. when this is not the case , A(3,4) and B(4,2) (or any other combinations) we can determine the
number of elements of the resultant matrix( in case of matrix multiplication) as below:
lets say m1- no. of rows in Matrix A, and n1 no of columns in Matrix A.
Simillarly m2 - no. of rows in Matrix B and n2 no of columns in Matrix B.

Matrix A has m1 * n1 elements and Matrix B has m2 * n2 elements. Lets say resultant Matrix is Matrix R whose number of elements can be determined as follows:

m1 * n1
m2 * n2 if n1 is equal to m2, we can write as below dashed line.]
-----------------
m1 * n2 this provides no of elements in the final matrix.(m1 rows and n2 cols)
-----------------

OR

m1 * n1
m2 * n2 if n2 is equal to m1, we can write as below dashed line.
-------------
m2 * n1 this provides no of elements in the final matrix.(m2 rows and n1 cols)
-------------

m1 n1 m2 n2
^ ^ ^ ^
for example A(rows,columns) that is , A(3,4) and B(4,2)

3 * 4
4 * 2
-----------------
3 * 2
-----------------

the product of A(3,4) and B(4,2) is R(3,2). this help us to predict number of rows , columns and elements in the resultant matrix. How do solve this programatically.

## All 2 Replies

I am learning java through a book. After finishing each chapter, i work out problems given at end of each chapter . when i tried to solve a problem , i could not frame the logic exactly. Please help to solve following exercise :
Write a program to find the product of two matrices A(3,4) and B(4,2).

I tried in code as below:

``````/* Exercise 5.14 Write a program to find the product of two matrices A(3,4) and
B(4,2) page : 71
*/
class Matrix	{
public static void main(String args[]) {
int i,j,k;
int r[][] = new int[3][2]; // resultant array is used  to store product
int a[][] = { //A(3,4)
{1,2,3,4},
{5,6,7,8},
{9,1,2,3}
};
int b[][] ={  // B(4,2)
{1,2},
{3,4},
{5,6},
{7,8}
};
// i dont know how to handle loops in this case.
for(i=0;i<2;i++)	{
for(j=0;j<3;j++)	{
for(k=0;k<4;i++)	{
r[j][i] += ( a[j][k] * b[k][i]);
}
}
}
// finally displaying the product matrix.
for(i=0;i<3;i++)	{
for(j=0;j<2;j++)	{
System.out.println(r[i][j]+"\t");
}
System.out.print("\n");
}
}
}``````

Its easy when two matrices(A(3,3) and B(3,3)) have same number of rows and columns. when this is not the case , A(3,4) and B(4,2) (or any other combinations) we can determine the
number of elements of the resultant matrix( in case of matrix multiplication) as below:
lets say m1- no. of rows in Matrix A, and n1 no of columns in Matrix A.
Simillarly m2 - no. of rows in Matrix B and n2 no of columns in Matrix B.

Matrix A has m1 * n1 elements and Matrix B has m2 * n2 elements. Lets say resultant Matrix is Matrix R whose number of elements can be determined as follows:

m1 * n1
m2 * n2 if n1 is equal to m2, we can write as below dashed line.]
-----------------
m1 * n2 this provides no of elements in the final matrix.(m1 rows and n2 cols)
-----------------

OR

m1 * n1
m2 * n2 if n2 is equal to m1, we can write as below dashed line.
-------------
m2 * n1 this provides no of elements in the final matrix.(m2 rows and n1 cols)
-------------

m1 n1 m2 n2
^ ^ ^ ^
for example A(rows,columns) that is , A(3,4) and B(4,2)

3 * 4
4 * 2
-----------------
3 * 2
-----------------

the product of A(3,4) and B(4,2) is R(3,2). this help us to predict number of rows , columns and elements in the resultant matrix. How do solve this programatically.

Hmm might this be of help:http://www.roseindia.net/java/beginners/MatrixMultiply.shtml

If you have an array arr that's int[m][n]
then you can get the size as follows
m = arr.length;
n = arr[0].length;
now you know how to get m1,n1,m2,n2 the rest is just if tests and other stuff you already know how to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.