-
Notifications
You must be signed in to change notification settings - Fork 82
/
matrix.java
70 lines (61 loc) · 2.25 KB
/
matrix.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
package matrix_ops;
import java.util.Scanner;
public class matrix {
public final Scanner input = new Scanner(System.in);
public void fillMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.printf("Line %d Column %d: ", i + 1, j + 1);
matrix[i][j] = input.nextInt();
}
}
}
public int[][] sumMatrix(int[][] matrix1, int[][] matrix2) {
int[][] sumOfMatrix = new int[matrix1.length][matrix1.length];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1.length; j++) {
sumOfMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return sumOfMatrix;
}
public int[][] subtractMatrix(int[][] matrix1, int[][] matrix2) {
int[][] addedMatrix = new int[matrix1.length][matrix1.length];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1.length; j++) {
addedMatrix[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
return addedMatrix;
}
public void printMatrix(int[][] matrix) {
int i, j;
for (i = 0; i < matrix.length; i++) {
System.out.println();
for (j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
}
System.out.println("");
}
public int[][] transposeMatrix(int[][] matrix) {
int[][] transposeMatrix = new int[matrix.length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
transposeMatrix[i][j] = matrix[j][i];
}
}
return transposeMatrix;
}
public int[][] multiplyMatrix(int[][] matrix1, int[][] matrix2) {
int[][] multiplyMatrix = new int[matrix1.length][matrix1.length];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
for (int k = 0; k < matrix1.length; k++) {
multiplyMatrix[k][j] = matrix1[i][0] * matrix2[0][j];
}
}
}
return multiplyMatrix;
}
}