When i multiply matrix 1 and 5 or 7 and 11 ,,, i get Matrix7 multiply Matrix11 3
ID: 3538419 • Letter: W
Question
When i multiply matrix 1 and 5 or 7 and 11 ,,, i get
Matrix7 multiply Matrix11
3.0 6.0 0.0
7.0 12.0 0.0
HOW DO I GET RID OF THE last column 0.0
0.0
it should be 2 x 2 matrix after multiplication
Matrix1
0 0 1
1 1 1
Matrix2
0 0 1
2 2 2
Matrix4
0 0 1
5 5 5
Matrix5
1 2
2 4
3 6
Matrix7
0 0 1
1 1 1
Matrix8
0 0 1
2 2 2
Matrix10
0 0 1
5 5 5
Matrix11
2 2
2 4
3 6
__________________________________________________________
pblic Matrix<T, A> multiply(Matrix<T, A> right) throws MatrixException {
Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize);
if (rowSize != right.columnSize)
throw new MatrixException(
"Cannot multiply matrices of different sizes");
for (int i = 0; i < rowSize; ++i) {
for (int j = 0; j < right.columnSize; ++j)
for(int k = 0; k < right.rowSize ; k++)
temp.matrix[i][j] = arithmetics.add(temp.matrix[i][j], (arithmetics.multiply(matrix[i][k], right.matrix[k][j])));
JUST AN EXAMPLE OF ADD
___________________________________
public Matrix<T, A> add(Matrix<T, A> right) throws MatrixException {
Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize);
if (rowSize != right.rowSize || columnSize != right.columnSize)
throw new MatrixException("Cannot add matrices of different sizes");
setup(temp, rowSize, columnSize);
for (int i = 0; i < rowSize; ++i) {
for (int j = 0; j < columnSize; ++j)
temp.matrix[i][j] = arithmetics.add(matrix[i][j],
right.matrix[i][j]);
}
return temp;
}
Explanation / Answer
I think you should use i++ and j++ instead of ++i and ++j
because you are using new values of i and j rather than current i, j
I wrote same code for matrix mult,
import java.util.*;
public class Mukesh
{
public static void main(String[] args)
{
int[][] A=new int[100][100];
int[][] B=new int[100][100];
int[][] C=new int[100][100];
int rows=5;
int column=5;
for(int i=0;i<rows;i++)
{
for(int j=0;j<column;j++)
{
A[i][j]=i+j+1;
B[i][j]=i+j+1;
C[i][j]=0;
}
}
int sum=0;
for (int c = 0 ; c < rows ; c++ )
{
for (int d = 0 ; d < column ; d++ )
{
for (int k = 0 ; k < column ; k++ )
{
sum = sum + A[c][k]*B[k][d];
}
C[c][d] = sum;
sum = 0;
}
}
for (int c = 0 ; c < rows ; c++ )
{
for (int d = 0 ; d < column ; d++ )
{
System.out.print(C[c][d]+" ");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.