63 lines
1.5 KiB
Java
63 lines
1.5 KiB
Java
public class Matrix {
|
|
private int[][] m;
|
|
|
|
public void init(int[][] base_m) {
|
|
this.m = base_m;
|
|
}
|
|
|
|
public int[][] getMatrix() {
|
|
return this.m;
|
|
}
|
|
|
|
public void print() {
|
|
for (int[] ml : this.m) {
|
|
for (int e : ml) {
|
|
System.out.printf("%4d ", e);
|
|
}
|
|
System.out.println();
|
|
}
|
|
}
|
|
|
|
public void transpose() {
|
|
if (this.m[0].length != this.m.length) {
|
|
int new_h = this.m[0].length;
|
|
int new_w = this.m.length;
|
|
|
|
int[][] new_m = new int[new_h][new_w];
|
|
|
|
for (int i = 0; i < this.m.length; i++) {
|
|
for (int j = 0; j < this.m[i].length; j++) {
|
|
new_m[j][i] = this.m[i][j];
|
|
}
|
|
}
|
|
|
|
this.m = new_m;
|
|
} else {
|
|
// if a matrix is square, there is no need to
|
|
// make a new one
|
|
|
|
int temp_value_holder;
|
|
|
|
for (int i = 0; i < this.m.length; i++) {
|
|
for (int j = 0; j < i; j++) {
|
|
temp_value_holder = this.m[i][j];
|
|
this.m[i][j] = this.m[j][i];
|
|
this.m[j][i] = temp_value_holder;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public double getAvg() {
|
|
double total = 0;
|
|
|
|
for (int[] ml : this.m) {
|
|
for (int e : ml) {
|
|
total += e;
|
|
}
|
|
}
|
|
|
|
return total / (this.m.length * this.m[0].length);
|
|
}
|
|
}
|