Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Matrix multiplication problem with different dimensions

1. ## Matrix multiplication problem with different dimensions

Hi I'm trying to finish my program where I can either sum or multiply a matrix. The issue I'm having atm is that I can't figure out how to multiply them if I have 4 variables, 2 for the size in row and column in the first matrix, and another 2 for the row and size in the second matrix. This is the complete code:

```import java.io.BufferedReader;
public class MatrizVaria{

public static int x,y,o,p;

public void Fill(int a[][]){
int i,j;
for (i=0;i<x;i++)
for (j=0;j<y;j++)
a[i][j] = (int)(Math.random()*10+1);
}
public void Sum(int a[][],int b[][],int c[][]){
int i,j;
for (i=0;i<x;i++)
for (j=0;j<y;j++)
c[i][j] = a[i][j] + b[i][j];
}

public void Product(int a[][], int b[][], int c[][]) {
int i, j, k, l;
for (i=0;i<x;i++) {
for (j=0;j<y;j++) {
c[i][j] = 0;
for (k=0;k<x;k++) {
for (l=0;l<y;l++) {
c[i][j] += a[i][x]*b[x][j];
c[i][j] += a[y][j]*b[i][y];
}
}
}
}
}
public void Print(int a[][]){
int i,j;
for (i=0;i<x;i++){
for (j=0;j<y;j++)
System.out.print("\t" + a[i][j]);
System.out.println();
}
}
public static void main(String[] arg) throws Exception {

String carac;

do
{
System.out.print("\nDo you want to Sum(s) or Multiply(m) a matrix?: ");
if (carac.equals("s"))
{
try{
System.out.println("Input the row size: ");
System.out.println("Input the column size: ");

int[][] Mat = new int[x][y];
int[][] Mat1 = new int[x][y];
int[][] Mat2 = new int[x][y];
MatrizVaria obj = new MatrizVaria();
obj.Fill(Mat);
obj.Fill(Mat1);
obj.Sum(Mat,Mat1,Mat2);

System.out.println("The first Matrix is: ");
obj.Print(Mat);
System.out.println("The second Matrix is: ");
obj.Print(Mat1);
System.out.println("The sum of the Matrixes is : ");
obj.Print(Mat2);
}catch(NumberFormatException nfe){
System.out.println(" Incorrect Input ");
}
}
if (carac.equals("m"))
{
try{
System.out.println("Input the row size of the first Matrix: ");
System.out.println("Input the column size of the first Matrix: ");
System.out.println("Input the row size of the second Matrix: ");
System.out.println("Input the column size of the second Matrix: ");

int[][] Mat = new int[x][y];
int[][] Mat1 = new int[o][p];
int[][] Mat2 = new int[x][y];
MatrizVaria obj = new MatrizVaria();
obj.Fill(Mat);
obj.Fill(Mat1);
obj.Product(Mat,Mat1,Mat2);

System.out.println("The first Matrix is: ");
obj.Print(Mat);
System.out.println("The second Matrix is: ");
obj.Print(Mat1);
System.out.println("The product of both Matrixes is: ");
obj.Print(Mat2);
}catch(NumberFormatException nfe){
System.out.println(" Incorrect Input ");
}
}
else
{
System.out.print("Invalid Option");
}

System.out.print("\nWould you like to perform another operation (y/n)?: ");
}
while(carac.equals("y"));
if(carac.equals("n"));
System.exit(0);
}
}```

The part of it i want to finish is:

```    public void Product(int a[][], int b[][], int c[][]) {
int i, j, k, l;
for (i=0;i<x;i++) {
for (j=0;j<y;j++) {
c[i][j] = 0;
for (k=0;k<x;k++) {
for (l=0;l<y;l++) {
c[i][j] += a[i][x]*b[x][j];
c[i][j] += a[y][j]*b[i][y];
}
}
}
}
}```
```if (carac.equals("m"))
{
try{
System.out.println("Input the row size of the first Matrix: ");
System.out.println("Input the column size of the first Matrix: ");
System.out.println("Input the row size of the second Matrix: ");
System.out.println("Input the column size of the second Matrix: ");

int[][] Mat = new int[x][y];
int[][] Mat1 = new int[o][p];
int[][] Mat2 = new int[x][y];
MatrizVaria obj = new MatrizVaria();
obj.Fill(Mat);
obj.Fill(Mat1);
obj.Product(Mat,Mat1,Mat2);

System.out.println("The first Matrix is: ");
obj.Print(Mat);
System.out.println("The second Matrix is: ");
obj.Print(Mat1);
System.out.println("The product of both Matrixes is: ");
obj.Print(Mat2);
}catch(NumberFormatException nfe){
System.out.println(" Incorrect Input ");
}
}```

2. ## Re: Matrix multiplication problem with different dimensions

I can't figure out how to multiply them if
Do you have the algorithm for solving your problem?
Once you get that, someone can help you code it in java.

3. ## Re: Matrix multiplication problem with different dimensions

Originally Posted by Norm
Do you have the algorithm for solving your problem?
Once you get that, someone can help you code it in java.
I know that in a matrix of n sides and n columns, n being the same number obviously, then the output for the class Product would be this:

```    public void Product(int a[][], int b[][], int c[][]) {
int i, j, k;
for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++) {

c[i][j] += a[i][k] * b[k][j];```

And the result I've confirmed it to be correct. However I'm having trouble figuring out how to implement this to different size matrixes.

4. ## Re: Matrix multiplication problem with different dimensions

You need a definition for how to multiply different sized matrixes.

5. ## Re: Matrix multiplication problem with different dimensions

Originally Posted by Norm
You need a definition for how to multiply different sized matrixes.
its like this:

matrixproduct.jpg

sorry for the crappy image. You multiply the row of the first matrix by the column of the second.

If the number of columns of the 1st matrix doesn't match the number of rows of the 2nd matrix then the product is not possible.

6. ## Re: Matrix multiplication problem with different dimensions

Have you written it down showing the r,c for the input matrixes and the output matrixes to get the pattern for writing the loops?
Something like this (I'm making up the indexes):
0,0 = 0,0 * 0,0
0,1 = 0,1 * 1,0

Your posted image shows the contents of the matrix vs the indexes for contents.