Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Algorithm for multiplting two int[][]

Thread view: 
bunallo - 28 Feb 2005 12:38 GMT
I am trying to make the product of two int[][] (matrices).

If I have the two matrices a and b I would like to calculate matrix c which
is the product of a and b:
In this example I use two matrices with length 2 that contains the same
numbers.

a: 1 2      b:1  2        =  c:  7  10
  3 4          3 4               15  22

Here I create matrix a and matrix b:

   int[][] a = new int[2][2];
   int[][] b = new int[2][2];

   int fill = 1;
   for (int i = 0; i < 2; i++){
     for (int k = 0; k < 2; k++){
       a[i][k] = fill;
       b[i][k] = fill;
       fill++;
     }
   }

Here I try yo make the calculation:

   int result = 0;
   int result2 = 0;
   int[]store = new int[4]; // Where I store the values of the product
matrix x.

   for (int m = 0; m < 2; m++){
     for (int n = 0; n < 2; n++){
       result = result + (a[m][n] * b[n][m]);
     }
     result2 = result;
     store[m*m] = result2; // 7 and 22 get stored in my array.
     result = 0;
   }
 }
}

But I can only seem to calculate "7" and "22"! Does anybody have a hint for
how to get the other two result of c calculated??
John - 28 Feb 2005 14:02 GMT
> I am trying to make the product of two int[][] (matrices).
>
[quoted text clipped - 40 lines]
> But I can only seem to calculate "7" and "22"! Does anybody have a hint for
> how to get the other two result of c calculated??

There is no need to reinvent the wheel.

http://math.nist.gov/javanumerics/jama/

John
Chris Smith - 28 Feb 2005 14:16 GMT
> Here I try yo make the calculation:
>
[quoted text clipped - 16 lines]
> But I can only seem to calculate "7" and "22"! Does anybody have a hint for
> how to get the other two result of c calculated??

If you're looking for the matrix cross product, then you need to
reconsider the logic a bit.  Essentially, when computing a cell of the
result, there is a row and a column, and then a third number that stands
in for row/column of the other matrix.  That's three nested loops.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.