forked from HarvardWestlake/SourceCodeTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixExample.java
More file actions
54 lines (42 loc) · 1.7 KB
/
MatrixExample.java
File metadata and controls
54 lines (42 loc) · 1.7 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
import java.util.Random;
public class MatrixExample {
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int rows2 = matrix2.length;
int cols2 = matrix2[0].length;
if (rows2 != cols1) {
throw new IllegalArgumentException(
"Number of columns in the first matrix must be equal to the number of rows in the second matrix.");
}
// Some more issues here too
int[][] result = new int[rows1][cols2];
// for (int i = 0; i < rows1; i++) {
// for (int j = 0; j < cols1; j++) {
// for (int k = 0; k < matrix1[i].length; k++) {
// result[i][j] = matrix1[i][k];
// }
// }
// }
// Lots of issues with this code, it used to be working perfectly though
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
System.out.println(result.length);
result[i][k] += matrix1[i][j] * matrix2[k][j];
}
}
}
return result;
}
public static int[][] generateRandomMatrix(int numRows, int numCols) {
int[][] matrix = new int[numRows][numCols];
Random random = new Random();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
matrix[i][j] = random.nextInt(100); // Generates random values between 0 and 99
}
}
return matrix;
}
}