Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

matrix multiplication

  Asked By: Gail    Date: Jun 01    Category: Java    Views: 2112
  

i ve been given task for writing a program for matrix
traversing ,addtion ,subtraction .........i did these three task
easiy but the fourth one..went over my head and i am trying to do it
my self for the last 3 dayz but couldnt find the correct way and was
getting incorrect answers. ..the TASK was to multiply tw0 matrix of
any dimension...i cant convert the multipication logic.. in to java
code ...so once again i need your guyzzz help.........
this

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Jarvia Miller     Answered On: Jun 01

if your matrix  has large amounts of data that you are manupulating then it's
mush more efficient that you prefer native based api 's for this operation.

 
Answer #2    Answered By: Allan Bailey     Answered On: Jun 01

this code do the stuff.
any length matrix  in 2 dimension


public static int[][] multiplicate(int[][] a, int[][] b)
{
// Matrix a has m rows, and n cols
// Matrix b has n rows, and p cols
int m = a.length;
int n = ((int[])a[0]).length;
int p = ((int[])b[0]).length;
int[][] result = new int[m][p];

for (int i = 0; i < m; i++)
{
for (int j = 0; j < p; j++)
{
int partial = 0;
for (int h = 0; h < n; h++)
{
partial += a[i][h] * b[h][j];
}
res[i][j] = partial;
}
}

return result;
}

 
Didn't find what you were looking for? Find more on matrix multiplication Or get search suggestion and latest updates.




Tagged: