|
-
February 10th, 2010, 03:04 PM
#3
Re: Matrix Multiplication
Okay, your last post helped me a lot in understanding this algorithm. My next question is how do I keep track of M. I get that it needs to be the location of the multiplication that is taking place. Also, what about the addition of the numbers after multiplication, when is that taken care of?
Take these 2 x 2 matrices for instance:
[ 4, 1 ] * [ 1, -2 ] = [ 4*1+1*4, 4*-2+1*-2] = [ 8, -10 ]
[ 5, 3 ] * [ 4, -2 ] = [ 5*1+3*4, 5*-2+3*-2] = [ 17, -16 ]
Note: This code does not currently compile due to the fact that I have yet to set up M as a variable.
import java.util.*;
import java.lang.Object;
class matrixX
{
public static void main (String args[])
{
int n=0;
Scanner scan = new Scanner(System.in);
System.out.println("The array will be n x n. What do you want n to be?");
n=scan.nextInt();
while (n<2)
{
System.out.println("Error: Value not greater than 2. Try Again: ");
n=scan.nextInt();
}
int[][] number1 = new int[n][n];
for (int i=0; i<number1.length; i++)
{
for (int j=0; j<number1[i].length; j++)
{
number1[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number1[i][j]+" ");
}
System.out.println(" ");
}
int x=number1.length;
System.out.println();
int[][] number2 = new int[n][n];
for (int i=0; i<number2.length; i++)
{
for (int j=0; j<number2[i].length; j++)
{
number2[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number2[i][j]+" ");
}
System.out.println(" ");
}
int y=number2.length;
int[][] answer = new int[n][n];
for (int row=0; row<answer.length; row++)
{
for(int col=0; col<answer.length; col++)
{
answer[row][col]=number1[row][M]*number2[M][col];
System.out.print(answer[row][col]+"\t");
}
System.out.println();
}
}
}
Last edited by Dexter-fan; February 10th, 2010 at 07:36 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|