Matrix multiplication in Java

Asked

Viewed 2,051 times

2

I need to make two matrices multiply, but I’m wrong when it comes to multiplying.

    int ma[][] = new int [3][2];
    int mb[][] = new int [2][2];
    int mab[][] = new int [3][2];

    for (int i = 0; i < ma.length; i++) {
        for (int j = 0; j < ma[i].length; j++) {
            System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 1");
            ma[i][j] = new Scanner(System.in).nextInt();
        }
    }

    for (int i = 0; i < mb.length; i++) {
        for (int j = 0; j < mb[i].length; j++) {
            System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 2");
            mb[i][j] = new Scanner(System.in).nextInt();
        }
    }

    mab[1][1] = (ma[1][1] * mb[1][1]) + (ma[1][2] * mb[2][1]);
    mab[1][2] = (ma[1][1] * mb[1][2]) + (ma[1][2] * mb[2][2]);
    mab[2][1] = (ma[2][1] * mb[1][1]) + (ma[2][2] * mb[1][2]);
    mab[2][2] = (ma[2][1] * mb[1][2]) + (ma[2][2] * mb[2][2]);
    mab[3][1] = (ma[3][1] * mb[1][1]) + (ma[3][2] * mb[2][1]);
    mab[3][2] = (ma[3][1] * mb[1][2]) + (ma[3][2] * mb[2][2]);

    System.out.println("Multipliacação das matrizes:");

    for (int i = 0; i < mab.length; i++) {
        for (int j = 0; j < mab[i].length; j++) {
            System.out.println(mab[i][j] + "\t");
        }
    }

In netbeans, it indicates that from the error on line 26, which is where you have the first multiplication, this message appears:

Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 2 At multiplication.Multiplication.main(Multiplication.java:26)

Did I miss the operation? I’m new to java

2 answers

5


When creating an array you pass its size as a parameter new int[tamanho]. The vector indices start from 0, according to the oracle documentation. You can also see in this Caelum link about vectors.

If you create a vector int vetor = new int[2] then he holds the positions vetor[0] and vetor[1].

In your case, when you created the int mb[][] = new int [2][2]; there is no index mb[X][2] there are the rates of mb[0][0] until mb[1][1]

You can make the following adjustment in the code:

//Seu código
mab[1][1] = (ma[1][1] * mb[1][1]) + (ma[1][2] * mb[2][1]);
//Ajuste
mab[0][0] = (ma[0][0] * mb[0][0]) + (ma[0][1] * mb[1][0]);

5

java.lang.Arrayindexoutofboundsexception

Means you are accessing a position outside the validated array positions.

In your case it happens here:

mab[3][2] = (ma[3][1] * mb[1][2]) + (ma[3][2] * mb[2][2]);

Indices of arrays in Java always start in 0, soon the ma which has been defined as:

int ma[][] = new int [3][2];

Has valid positions for [0 a 2][0 a 1].

However the logic you are making should be done through a loop/loop, so it can be dynamic and adaptable to any dimensions.

Follow an example calculation with for and hitting other details that were less well:

Scanner teclado = new Scanner(System.in); //criar o Scanner apenas uma vez

int ma[][] = new int[3][2];
int mb[][] = new int[2][2];
int mab[][] = new int[3][2];

for (int i = 0; i < ma.length; i++) {
    for (int j = 0; j < ma[i].length; j++) {
        System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 1");
        ma[i][j] = teclado.nextInt(); //ler com base no Scanner criado
    }
}

for (int i = 0; i < mb.length; i++) {
    for (int j = 0; j < mb[i].length; j++) {
        System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 2");
        mb[i][j] = teclado.nextInt(); //ler com base no Scanner criado
    }
}

//calculo da multiplicação das matrizes
for(int i=0; i<mab.length; i++){
    for(int j=0; j<mab[i].length; j++){
        for(int k=0; k<ma[i].length; k++){
            mab[i][j] += ma[i][k] * mb[k][j];
        }
    }
}

Example in Ideone

Also read another answer I gave about multiplication of matrices in C

Browser other questions tagged

You are not signed in. Login or sign up in order to post.