By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

multiplication of 2 matrix

P: 50
I am a java beginner and I was given this task to do.. to create a class which given 2 matrices,and these bein of the correct dimensions, will produce a third matrix by multiplying both. There is the need of a class Matrix which contains the following methods :

public Matrix multiply (Matrix multiplicand, Matrix multiplier); and

public void display();
The display() method will output a matrix upon the console, columns should be tabbed and
rows printed on a new line e.g.
20 6 112
1 -761 12
85 30 -65
The matrix may have a any number of columns or rows. Your Matrix class must be flexible enough to support this. Generate a program which randomly creates matrices and multiplies them, always displaying each matrix (the multiplicand, multiplier and the result..)

Can please some1 help me in dealing with this task? Thanks very much
Sep 18 '07 #1
Share this Question
Share on Google+
12 Replies


10K+
P: 13,264
I am a java beginner and I was given this task to do.. to create a class which given 2 matrices,and these bein of the correct dimensions, will produce a third matrix by multiplying both. There is the need of a class Matrix which contains the following methods :

public Matrix multiply (Matrix multiplicand, Matrix multiplier); and

public void display();
The display() method will output a matrix upon the console, columns should be tabbed and
rows printed on a new line e.g.
20 6 112
1 -761 12
85 30 -65
The matrix may have a any number of columns or rows. Your Matrix class must be flexible enough to support this. Generate a program which randomly creates matrices and multiplies them, always displaying each matrix (the multiplicand, multiplier and the result..)

Can please some1 help me in dealing with this task? Thanks very much
And what have you done so far?
Sep 18 '07 #2

Expert 10K+
P: 11,448
I am a java beginner and I was given this task to do.. to create a class which given 2 matrices,and these bein of the correct dimensions, will produce a third matrix by multiplying both. There is the need of a class Matrix which contains the following methods :

public Matrix multiply (Matrix multiplicand, Matrix multiplier); and

public void display();
The display() method will output a matrix upon the console, columns should be tabbed and
rows printed on a new line e.g.
20 6 112
1 -761 12
85 30 -65
The matrix may have a any number of columns or rows. Your Matrix class must be flexible enough to support this. Generate a program which randomly creates matrices and multiplies them, always displaying each matrix (the multiplicand, multiplier and the result..)

Can please some1 help me in dealing with this task? Thanks very much
So for matrixes A and B with n rows, m columns and m rows, p columns respectively,
what would be the size of matrix C == A*B?

How would you test whether or not A and B have the correct sizes? and how
do you calculate the dot product of row i of matrix A times column j of matrix B?

What have you calculated then? Have you thought about these sub-problems?

kind regards,

Jos
Sep 18 '07 #3

P: 50
what i know is that i need to build a matrix with 2 arrays [] []. The length of these arrays need to be randomly generated. Also the first element in the first array needs to be multiplied by the first element in the other array.

I dont know how to do this in java.. also i dont know how to randomly generate the length of an array.

Thanks

Regards

Christine
Sep 18 '07 #4

Expert 10K+
P: 11,448
I dont know how to do this in java.. also i dont know how to randomly generate the length of an array.
You do know how to program using the Java programming language don't you?

kind regards,

Jos
Sep 18 '07 #5

10K+
P: 13,264
what i know is that i need to build a matrix with 2 arrays [] []. The length of these arrays need to be randomly generated. Also the first element in the first array needs to be multiplied by the first element in the other array.

I dont know how to do this in java.. also i dont know how to randomly generate the length of an array.

Thanks

Regards

Christine
Before you start thinking about how to do stuff in Java, first get a Math tutorial and make sure you know how to multiply matrices manually. Note down all the special cases e.t.c
Then read a Java arrays tutorial. After that all you need to do is read the API for the java.util.Random class and you're done.

I guess you realize that the keyword in all that is read.
You can always post back here at any time that you get stuck with any of the above.
Sep 18 '07 #6

P: 41
Hi,

To find the Product of two Matrices, First we've to verify the matrix sizes, for this we've a Condition like No.of Columns of First Matrix (A) should be equla to No.of Rows in Second Matrix (B) then in the resultant matrix (C), No.of Rows should be equal to the no.of Rows in First Matrix(A) and the no.of Columns of Resultant Matrix (C) should be equal to the no.of Columns of Second Matrix (B).

For example, A is a Matrix with the size of 3x2, and B is a Matrix with the size of 2x4 then the Resultant Matrix (C) size should be 3x4 (No.of Rows in A x No.of Columns in B) as These matrices satisfy the condition as no.of Columns of A are equal to No.of Rows in B)

so go ahead,

Cheers,
Sateesh.
Sep 19 '07 #7

P: 50
I' ve done the following code.. but I dont know how to randomly generate the numbers and the length of the arrays.. The length of the arrays need to have certain restriction that the first matrix must have number of columns equal to the number of rows of the second matrix.

public class Matrix {

// the dimensions for the matrix

// Matrix dimensions
int nRowsMatrixA = 2; // number of rows for matrix A
int nColsMatrixA = 3; // number of columns for matrix A
int nRowsMatrixB = 3; // number of rows for matrix B
int nColsMatrixB = 2; // number of columns for matrix B
int nRowsMatrixC = 2; // number of rows for matrix C
int nColsMatrixC = 2; // number of columns for matrix C

double [][] matrixA = new double [nRowsMatrixA][nColsMatrixA];
double [][] matrixB = new double [nRowsMatrixA][nColsMatrixB];
double [][] matrixC = new double [nRowsMatrixC][nRowsMatrixC];

Random rand = new Random();
matrixA [0][0] = rand.nextInt(20);







public Matrix multiply (Matrix multiplicand, Matrix multiplier) {



}
}
Sep 21 '07 #8

Nepomuk
Expert 2.5K+
P: 3,112
I' ve done the following code.. but I dont know how to randomly generate the numbers and the length of the arrays..
For random numbers, use java.util.Random.

About the issue of creating compatible Matrixes, you'll have to either read the lengths from the first Matrix while creating the second one or save them before creating the first Matrix and reuse them in the second one.

Greetings,
Nepomuk
Sep 21 '07 #9

P: 2
well i jst tried 2 get this code done fr my college tutorials!....
multiplication of matrices wid unequal orders!
works great....jst get thru d logic done!
lol...its huge...ignore d strngs fr the user plz! :P
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4. int i,j,k,x=0,ai,aj,bi,bj,a[10][10],b[10][10],c[10][10];
  5. wrong:
  6. printf("enter in the order(ixj) of matrix A\n");
  7. scanf("%d",&ai);
  8. scanf("%d",&aj);
  9. printf("\nnow the order(ixj) of matrix B\n");
  10. scanf("%d",&bi);
  11. scanf("%d",&bj);
  12. printf("\nOrder of matrix A = (%dx%d)\n",ai,aj);        //display orders of
  13. printf("Order of matrix B = (%dx%d)\n\n",bi,bj);    //of A n B!
  14.  
  15. if(aj!=bi)
  16. {
  17. printf("The resultant product matrix wont exist as Ai and Bj aren't equal!\n");
  18. printf("\n\nEnter in the orders again!\n");
  19. goto wrong;
  20. }
  21.  
  22. printf("Enter the matrix 'A'(row by row)\n\n");        //input fr matrix A!
  23. for(i=0;i<ai;i++)
  24. {
  25. printf("Row%d\n",i+1);
  26.     for(j=0;j<aj;j++)
  27.     scanf("%d",&a[i][j]);
  28. }
  29.  
  30.  
  31. printf("\nEnter the matrix 'B'(row by row)\n\n");        //input fr matrix B!
  32. for(i=0;i<bi;i++)
  33. {
  34. printf("Row%d\n",i+1);
  35.     for(j=0;j<bj;j++)
  36.     scanf("%d",&b[i][j]);
  37. }
  38.  
  39. printf("\n\n\nthe matrices of multiplication are!\n\nA:");
  40. for(i=0;i<ai;i++)                    //display A matrix!
  41. {
  42. printf("\t");
  43.     for(j=0;j<aj;j++)
  44.     printf("%d\t",a[i][j]);
  45. printf("\n\n");
  46. }
  47.  
  48. printf("\n\nB:");                                //display B matrix!
  49. for(i=0;i<bi;i++)
  50. {
  51. printf("\t");
  52.     for(j=0;j<bj;j++)
  53.     printf("%d\t",b[i][j]);
  54. printf("\n\n");
  55. }
  56.  
  57.  
  58. for(i=0;i<ai;i++)                //multiplicatn(main logic)
  59. {
  60.     for(j=0;j<bj;j++)
  61.     {
  62.     x=0;
  63.         for(k=0;k<aj;k++)
  64.         x=x+a[i][k]*b[k][j];
  65.     c[i][j]=x;
  66.     }
  67. }
  68.  
  69.  
  70.  
  71. printf("\n\nThe multiplication of the above matrices A & B....C:\n\nA x B = ");
  72. for(i=0;i<ai;i++)                //display d product result!
  73. {
  74. printf("\t");
  75.     for(j=0;j<bj;j++)
  76.     printf("%d\t",c[i][j]);
  77. printf("\n\n\t");
  78. }
  79.  
  80. }
  81.  
Sep 30 '07 #10

Expert 10K+
P: 11,448
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.  
Oh dear; this is a Java forum and your source code isn't valid C or C++ either.

kind regards,

Jos
Sep 30 '07 #11

P: 1
i would like to know if it is possible to find a scalar r if we multiplythe matrixesM and N such that MN=rN
M= 3 -0.50 0.50 and matrix N= 6.75
0 6.50 -8 13.5
0 4 -5. 6.750
PLEASE HELP
Oct 10 '07 #12

Expert 10K+
P: 11,448
i would like to know if it is possible to find a scalar r if we multiplythe matrixesM and N such that MN=rN
M= 3 -0.50 0.50 and matrix N= 6.75
0 6.50 -8 13.5
0 4 -5. 6.750
PLEASE HELP
Read up on 'eigen values' and 'eigen vectors' here. btw, you should have
started your own thread for this; hijacking is considered impolite and not done.

kind regards,

Jos
Oct 10 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.