-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
57 lines (44 loc) · 1.35 KB
/
Matrix.java
File metadata and controls
57 lines (44 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class Matrix {
public static int[][] multiply(int[][] m1, int[][] m2) {
if (m1[0].length != m2.length) {
return null;
}
int[][] res = new int[m1.length][m2[0].length];
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m2[i].length; j++) {
int sum = 0;
for (int k = 0; k < m2[i].length; k++) {
sum += m1[i][k] * m2[k][j];
}
System.out.println();
res[i][j] = sum;
}
}
return res;
}
public static void print(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] arr = new int[2][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
// print(arr);
int[][] m1 = new int[2][2];
m1[0][0] = 1;
m1[0][1] = 2;
arr[1][0] = 3;
arr[1][1] = 4;
int[][] res = multiply(m1, m1);
print(res);
}
}