对于作业,我必须从文本文件(matrix.txt)中获得两个矩阵A和B。然后必须将矩阵拆分为子矩阵
ex
A = {{A00, A01}, {A10, A11}}
对矩阵A和B完成一次后,我必须将两个对应的子矩阵加在一起以获得矩阵C的子矩阵。
例如。
C00 = (A00 + B00)
C01 = (A01 + B01)
C10 = (A10 + B10)
C11 = (A11 + B11)
C = {{C00, C01}, {C10, C11}}
除了将C子矩阵合并回到矩阵C中之外,我已经能够完成所有工作。
这是matrix.txt的示例
4 7
2 3 1 2 5 1 2
3 1 2 2 2 4 4
1 2 3 2 7 2 1
3 6 1 5 1 3 5
6 5 4 1 4 3 1
3 3 2 2 1 1 2
7 5 4 3 2 5 3
2 1 8 4 8 4 4
到目前为止,这是我的代码
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
public class MatrixThread {
public static Scanner scan;
public static void main(String[] args) {
if(args.length == 0){
System.out.println("File name not specified");
System.exit(1);
}
try{
File matrix = new File(args[0]);
scan = new Scanner(matrix);
} catch (IOException ioException) {
System.err.println("Cannot open file.");
System.exit(1);
}
ThreadOperation Operation = new ThreadOperation();
Operation.executeOperation();
}
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
mport java.util.Arrays;
class ThreadOperation extends MatrixThread {
void executeOperation(){
int N = scan.nextInt();
int M = scan.nextInt();
int A[][] = new int [N][M];
int B[][] = new int [N][M];
//System.out.println(N + " " + M);
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < M; ++j)
{
if(scan.hasNextInt())
{
A[i][j] = scan.nextInt();
}
}
}
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < M; ++j)
{
if(scan.hasNextInt())
{
B[i][j] = scan.nextInt();
}
}
}
/*int C[][] = new int [N][M];
for (int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
C[i][j] = A[i][j] + B[i][j];
System.out.print(C[i][j] + " ");
}
System.out.print("\r\n");
}*/
int halfRow = N/2;
int halfCol = M/2;
int remainingRow = N - halfRow;
int remainingCol = M - halfCol;
//System.out.print(halfRow + " " + halfCol + " " + remainingRow + " " + remainingCol);
//System.out.println(" ");
//System.out.println(" ");
int A00[][] = new int[halfRow][halfCol];
for (int i = 0; i < halfRow; i++){
for (int j = 0; j < halfCol; j++){
A00[i][j] = A[i][j];
//System.out.print(A00[i][j] + " ");
}
//System.out.print("\r\n");
}
//System.out.println(" ");
int A10[][] = new int[remainingRow][halfCol];
for (int i = halfRow; i < N; i++){
for (int j = 0; j < halfCol; j++){
A10[i - halfRow][j] = A[i][j];
//System.out.print(A10[i - halfRow][j] + " ");
}
//System.out.print("\r\n");
}
//System.out.println(" ");
int A01[][] = new int[halfRow][remainingCol];
for (int i = 0; i < halfRow; i++) {
for (int j = halfCol; j < M; j++){
A01[i][j - halfCol] = A[i][j];
//System.out.print(A01[i][j - halfCol] + " ");
}
//System.out.print("\r\n");
}
//System.out.println(" ");
int A11[][] = new int[remainingRow][remainingCol];
for (int i = halfRow; i < N; i++) {
for(int j = halfCol; j < M; j++) {
A11[i - halfRow][j - halfCol] = A[i][j];
//System.out.print(A11[i - halfRow][j - halfCol] + " ");
}
//System.out.print("\r\n");
}
//System.out.println(" ");
int B00[][] = new int[halfRow][halfCol];
for (int i = 0; i < halfRow; i++){
for (int j = 0; j < halfCol; j++){
B00[i][j] = B[i][j];
//System.out.print(B00[i][j] + " ");
}
//System.out.print("\r\n");
}
//System.out.println(" ");
int B10[][] = new int[remainingRow][halfCol];
for (int i = halfRow; i < N; i++){
for (int j = 0; j < halfCol; j++){
B10[i - halfRow][j] = B[i][j];
//System.out.print(B10[i - halfRow][j] + " ");
}
//System.out.print("\r\n");
}
//System.out.println(" ");
int B01[][] = new int[halfRow][remainingCol];
for (int i = 0; i < halfRow; i++) {
for (int j = halfCol; j < M; j++){
B01[i][j - halfCol] = B[i][j];
//System.out.print(B01[i][j - halfCol] + " ");
}
//System.out.print("\r\n");
}
//System.out.println(" ");
int B11[][] = new int[remainingRow][remainingCol];
for (int i = halfRow; i < N; i++) {
for(int j = halfCol; j < M; j++) {
B11[i - halfRow][j - halfCol] = B[i][j];
//System.out.print(B11[i - halfRow][j - halfCol] + " ");
}
//System.out.print("\r\n");
}
//System.out.println();
int C00[][] = new int [halfRow][halfCol];
for (int i = 0; i < halfRow; i++){
for ( int j = 0; j < halfCol; j++){
C00[i][j] = A00[i][j] + B00[i][j];
System.out.print(C00[i][j] + " ");
}
System.out.println();
}
int C01[][] = new int [halfRow][remainingCol];
for (int i = 0; i < halfRow; i++){
for ( int j = 0; j < remainingCol; j++){
C01[i][j] = A01[i][j] + B01[i][j];
System.out.print(C01[i][j] + " ");
}
System.out.println();
}
int C10[][] = new int [remainingRow][halfCol];
for (int i = 0; i < remainingRow; i++){
for ( int j = 0; j < halfCol; j++){
C10[i][j] = A10[i][j] + B10[i][j];
System.out.print(C10[i][j] + " ");
}
System.out.println();
}
int [][] C0 = new int [halfRow][M];
int [][] C1 = new int [remainingRow][M];
System.arraycopy(C00, 0, C0, 0, remainingCol);
System.arraycopy(C10, 0, C1, 0, remainingCol);
System.arraycopy(C01, halfCol, C0, 0, remainingCol);
System.arraycopy(C11, halfCol, C1, 0, remainingCol);
System.out.print(Arrays.deepToString(C0));
System.out.print(Arrays.deepToString(C1));
}
}
我当前遇到的错误是java.lang.ArrayIndexOutOfBoundsException
这是基于我提供的示例matrix.txt文件的每个矩阵所需输出的示例
N = 4, M = 7
Matrix A:
2 3 1 2 5 1 2
3 1 2 2 2 4 4
1 2 3 2 7 2 1
3 6 1 5 1 3 5
Matrix B:
6 5 4 1 4 3 1
3 3 2 2 1 1 2
7 5 4 3 2 5 3
2 1 8 4 8 4 4
Submatrix A00:
2 3 1 2
3 1 2 2
Submatrix A01:
5 1 2
2 4 4
Submatrix A10:
1 2 3 2
3 6 1 5
Submatrix A11:
7 2 1
1 3 5
Submatrix B00:
6 5 4 1
3 3 2 2
Submatrix B01:
4 3 1
1 1 2
Submatrix B10:
7 5 4 3
2 1 8 4
Submatrix B11:
2 5 3
8 4 4
Submatrix C00:
8 8 5 3
6 4 4 4
Submatrix C01:
9 4 3
3 5 6
Submatrix C10:
8 7 7 5
5 7 9 9
Submatrix C11:
9 7 4
9 7 9
Matrix C:
8 8 5 3 9 4 3
6 4 4 4 3 5 6
8 7 7 5 9 7 4
5 7 9 9 9 7 9
答案 0 :(得分:0)
我可以看到下面的情况与预期相矛盾,
1. When Row, Col = {4, 7} A00={2, 4}, A01={2, 3}
因此可以如下修改计算
int halfRow = (N % 2 == 0) ? (N/2) : (N/2 + 1);
int halfCol = (M % 2 == 0) ? (M/2) : (M/2 + 1);
int remainingRow = N - halfRow;
int remainingCol = M - halfCol;
还
2. C11 calculation was missing, not sure if it was missed just while pasting the code in SO
因此,我在这里包括了这些
static void executeOperation() {
int N = scan.nextInt();
int M = scan.nextInt();
int[][] A = new int[N][M];
int[][] B = new int[N][M];
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (scan.hasNextInt()) {
A[i][j] = scan.nextInt();
}
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (scan.hasNextInt()) {
B[i][j] = scan.nextInt();
}
}
}
int halfRow = (N % 2 == 0) ? (N/2) : (N/2 + 1);
int halfCol = (M % 2 == 0) ? (M/2) : (M/2 + 1);
int remainingRow = N - halfRow;
int remainingCol = M - halfCol;
int[][] A00 = new int[halfRow][halfCol];
for (int i = 0; i < halfRow; i++) {
if (halfCol >= 0)
System.arraycopy(A[i], 0, A00[i], 0, halfCol);
}
int[][] A10 = new int[remainingRow][halfCol];
for (int i = halfRow; i < N; i++) {
if (halfCol >= 0)
System.arraycopy(A[i], 0, A10[i - halfRow], 0, halfCol);
}
int[][] A01 = new int[halfRow][remainingCol];
for (int i = 0; i < halfRow; i++) {
if (M - halfCol >= 0)
System.arraycopy(A[i], halfCol, A01[i], 0, M - halfCol);
}
int[][] A11 = new int[remainingRow][remainingCol];
for (int i = halfRow; i < N; i++) {
if (M - halfCol >= 0)
System.arraycopy(A[i], halfCol, A11[i - halfRow], 0, M - halfCol);
}
System.out.println(" ");
int[][] B00 = new int[halfRow][halfCol];
for (int i = 0; i < halfRow; i++) {
if (halfCol >= 0)
System.arraycopy(B[i], 0, B00[i], 0, halfCol);
}
int[][] B10 = new int[remainingRow][halfCol];
for (int i = halfRow; i < N; i++) {
if (halfCol >= 0)
System.arraycopy(B[i], 0, B10[i - halfRow], 0, halfCol);
}
int[][] B01 = new int[halfRow][remainingCol];
for (int i = 0; i < halfRow; i++) {
if (M - halfCol >= 0)
System.arraycopy(B[i], halfCol, B01[i], 0, M - halfCol);
}
int[][] B11 = new int[remainingRow][remainingCol];
for (int i = halfRow; i < N; i++) {
if (M - halfCol >= 0)
System.arraycopy(B[i], halfCol, B11[i - halfRow], 0, M - halfCol);
}
int[][] C00 = new int[halfRow][halfCol];
for (int i = 0; i < halfRow; i++) {
for (int j = 0; j < halfCol; j++) {
C00[i][j] = A00[i][j] + B00[i][j];
}
}
int[][] C01 = new int[halfRow][remainingCol];
for (int i = 0; i < halfRow; i++) {
for (int j = 0; j < remainingCol; j++) {
C01[i][j] = A01[i][j] + B01[i][j];
}
}
int[][] C10 = new int[remainingRow][halfCol];
for (int i = 0; i < remainingRow; i++) {
for (int j = 0; j < halfCol; j++) {
C10[i][j] = A10[i][j] + B10[i][j];
}
}
int[][] C11 = new int[remainingRow][remainingCol];
for (int i = 0; i < remainingRow; i++) {
for (int j = 0; j < remainingCol; j++) {
C11[i][j] = A11[i][j] + B11[i][j];
}
}
int[][] C0 = new int[halfRow][M];
int[][] C1 = new int[halfRow][M];
cloneArray(C00, 0, C0, 0);
cloneArray(C01, halfCol, C0, 0);
cloneArray(C10, 0, C1, 0);
cloneArray(C11, halfCol, C1, 0);
int[][] C = new int[N][M];
cloneArray(C0, 0, C, 0);
cloneArray(C1, 0, C, halfRow);
System.out.print(Arrays.deepToString(C));
}
private static void cloneArray(int[][] src, int destPos, int[][] target, int tarPos) {
int length = src.length;
for (int i = 0; i < length; i++) {
System.arraycopy(src[i], 0, target[i+tarPos], destPos, src[i].length);
}
}
输入:
4 7
2 3 1 2 5 1 2
3 1 2 2 2 4 4
1 2 3 2 7 2 1
3 6 1 5 1 3 5
6 5 4 1 4 3 1
3 3 2 2 1 1 2
7 5 4 3 2 5 3
2 1 8 4 8 4 4
输出:
[[8, 8, 5, 3, 9, 4, 3],
[6, 4, 4, 4, 3, 5, 6],
[8, 7, 7, 5, 9, 7, 4],
[5, 7, 9, 9, 9, 7, 9]]
希望有帮助。