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:
Code java:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MatrizVaria{
public static int x,y,o,p;
public static BufferedReader var = new BufferedReader(new InputStreamReader(System.in));
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?: ");
carac = var.readLine();
if (carac.equals("s"))
{
try{
System.out.println("Input the row size: ");
x = Integer.parseInt(var.readLine());
System.out.println("Input the column size: ");
y = Integer.parseInt(var.readLine());
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: ");
x = Integer.parseInt(var.readLine());
System.out.println("Input the column size of the first Matrix: ");
y = Integer.parseInt(var.readLine());
System.out.println("Input the row size of the second Matrix: ");
o = Integer.parseInt(var.readLine());
System.out.println("Input the column size of the second Matrix: ");
p = Integer.parseInt(var.readLine());
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)?: ");
carac = var.readLine();
}
while(carac.equals("y"));
if(carac.equals("n"));
System.exit(0);
}
}
The part of it i want to finish is:
Code java:
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];
}
}
}
}
}
Code java:
if (carac.equals("m"))
{
try{
System.out.println("Input the row size of the first Matrix: ");
x = Integer.parseInt(var.readLine());
System.out.println("Input the column size of the first Matrix: ");
y = Integer.parseInt(var.readLine());
System.out.println("Input the row size of the second Matrix: ");
o = Integer.parseInt(var.readLine());
System.out.println("Input the column size of the second Matrix: ");
p = Integer.parseInt(var.readLine());
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 ");
}
}
Please help me, I really need to finish this part. Thank you
Re: Matrix multiplication problem with different dimensions
Quote:
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.
Re: Matrix multiplication problem with different dimensions
Quote:
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:
Code java:
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.
Re: Matrix multiplication problem with different dimensions
You need a definition for how to multiply different sized matrixes.
1 Attachment(s)
Re: Matrix multiplication problem with different dimensions
Quote:
Originally Posted by
Norm
You need a definition for how to multiply different sized matrixes.
its like this:
Attachment 766
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.
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.