Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions MatrixExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public static void main(String[] args) {
{ 73, 18, 2, 2, 5, 11 }
};

int numRows = 6;
int numCols = 7;
int numRows = 7;
int numCols = 6;

int[][] matrix2 = generateRandomMatrix(numRows, numCols);
int[][] matrix2 = generateRandomMatrix(numCols, numRows);
int[][] result = multiplyMatrices(matrix, matrix2);

System.out.println("result length: " + result.length + " x " + result[0].length);
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
Expand All @@ -31,8 +31,8 @@ public static void main(String[] args) {
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;
int rows2 = matrix2[0].length;
int cols2 = matrix2.length;

if (cols1 != rows2) {
throw new IllegalArgumentException(
Expand All @@ -44,7 +44,7 @@ public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {

// 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 j = 0; j < cols1; j++) {
for (int k = 0; k < cols1; k++) {
result[j][k] += matrix1[i][j] * matrix2[k][j];
}
Expand Down
35 changes: 35 additions & 0 deletions MatrixExampleTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.Arrays;

public class MatrixExampleTester {
public static void main(String[] args) {
// int[][] one = MatrixExample.generateRandomMatrix(5, 5);
// int[][] two = MatrixExample.generateRandomMatrix(5, 5);
// int[][] multiplied = MatrixExample.multiplyMatrices(one, two);
// System.out.println(Arrays.deepToString(multiplied));

int[][] matrix = {
{ 1, 2, 3, 4, 5, 6 },
{ 4, 5, 6, 3, 7, 2 },
{ 27, 8, 9, 5, 3, 21 },
{ 73, 2, 19, 5, 1, 8 },
{ 47, 9, 9, 5, 0, 22 },
{ 78, 86, 1, 4, 1, 21 },
{ 73, 18, 2, 2, 5, 11 }
};

int numRows = 7;
int numCols = 6;

int[][] matrix2 = MatrixExample.generateRandomMatrix(numRows, numCols);
int[][] result = MatrixExample.multiplyMatrices(matrix, matrix2);

System.out.println("result length: " + result.length + " x " + result[0].length);
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}

}
}