Matrix Multiplication -> [2x4][3x1] = [2x1]
#What is the size of Array after operating Matrix Multiplication ?
#Forword
I don't know why i feel this, when i need to use Array in Java, I am confused how to use it.. This document also will handle the matrix multiplication, this document is also for my future use and used for organizing concepts. Surely, When I can't define something difficult, I am endeavoring to organize it, In doing so, I can understand how it works without realizing how can i understand this, I hope this works well !
#Matrix Multiplication
arr1[R1][C1] * arr2[R2][C2]
Requirement For this matrix multiplication.
when (C1 == R2), matrix multiplication can be operated.
First Matrix Column == Second Matrix Row
If (C1 == R2) {
arr1[R1][C1] * arr2[R2][C2] => resultArray[R1][C2]
}
more specified,
arr1[3][5] * arr2[2][1]
\==> Column of arr1 != Row of arr2
\==> This can't be Operated
arr1[2][4] * arr2[4][5]
\==> Column of arr1 == Row of arr2
\==> resultArr[2][5] (arr1 Row * arr2 Column)
#Code Snippets
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] arr1 = {{1,2},{13,15},{17,12}};
int[][] arr2 = {{2,1,5},{7,3,3}};
if (arr1[0].length != arr2.length) {
System.out.println(
"Number of columns in the first matrix must be equal to"
+ " the number of rows in the second matrix "
+ "for matrix multiplication");
//if this condition is false => exit main method by using "return;" with error message.
return;
}
int[][] result = new int[arr1.length][arr2[0].length];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2[0].length; j++) {
for (int k = 0; k < arr1[0].length; k++) {
result[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
System.out.println("result[" + i + "][" + j + "] = "
+ result[i][j] + " ");
}
}
System.out.println();
}
}
cf) In Java, "return;"
When condition is 'false', it will exit the current method.
it's important to note that return;
will only exit the current method, not the entire program.