446,190 Members | 765 Online 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
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  = rand.nextInt(20); public Matrix multiply (Matrix multiplicand, Matrix multiplier) { } } Sep 21 '07 #8

 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 #include void main() { int i,j,k,x=0,ai,aj,bi,bj,a,b,c; wrong: printf("enter in the order(ixj) of matrix A\n"); scanf("%d",&ai); scanf("%d",&aj); printf("\nnow the order(ixj) of matrix B\n"); scanf("%d",&bi); scanf("%d",&bj); printf("\nOrder of matrix A = (%dx%d)\n",ai,aj);        //display orders of printf("Order of matrix B = (%dx%d)\n\n",bi,bj);    //of A n B!   if(aj!=bi) { printf("The resultant product matrix wont exist as Ai and Bj aren't equal!\n"); printf("\n\nEnter in the orders again!\n"); goto wrong; }   printf("Enter the matrix 'A'(row by row)\n\n");        //input fr matrix A! for(i=0;i

 Expert 10K+ P: 11,448 Expand|Select|Wrap|Line Numbers #include void main() {   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 