Ask your own question, for FREE!
Mathematics 14 Online
OpenStudy (steve816):

Anybody good with java programming? I need help with multiplying matrices in java.

OpenStudy (518nad):

i can try to fix your code, im a bit rusty but ill see if the math is right

OpenStudy (steve816):

Ok, thank you for your willingness to help me. Here is my matrix multiplication method.

OpenStudy (steve816):

public class Matrix extends Object { // postcondition: all elements in the return value are in the range of 0...mod-1 public static int[][] mult(int[][] a, int[][] b, int mod) { int ra = a.length; int ca = a[0].length; int rb = b.length; int cb = b[0].length; int[][] mult = new int[ra][cb]; for (int r = 0; r < ra; r++) { for (int c = 0; c < cb; c++) { for (int i = 0; i < ca; i++) { mult[r][c] += a[r][i] * b[i][c]; } } } return mult; } public static void main(String[] args) { int[][] a = {{2, 1, 3}, {6, 5, 9}}, b = {{4, 6, 8}, {7, 1, 3}}; System.out.println("a*b="+java.util.Arrays.deepToString(Matrix.mult(a, b, 11))); //System.out.println("det(a)="+Matrix.det(a, 11)); } }

OpenStudy (steve816):

Copy paste this code to compilejava.net

OpenStudy (518nad):

hi im here

OpenStudy (518nad):

okay so lets do this

OpenStudy (steve816):

I need to know why I'm getting an index out of bounds error.

OpenStudy (518nad):

you mixing up col and rows

OpenStudy (steve816):

What? How?

OpenStudy (518nad):

okay so A.length ==?

OpenStudy (518nad):

hmm it looks right

OpenStudy (518nad):

try changing it to ra-1 see if it fixes it

OpenStudy (518nad):

oh wait it say index out of range for mult

OpenStudy (518nad):

you can try making sure the size of mult exists already

OpenStudy (518nad):

define mult to be a zero matrix with col of A and rows of B

OpenStudy (518nad):

its rows are Col of A and its col are rows of B

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!