簡體   English   中英

用Java添加矩陣

[英]Add Matrices in Java

我需要編寫有關如何添加兩個矩陣的簡短程序。第一個矩陣應如下所示:

1 2 3 4 5 6 7 8 9 10
11 12 13.......19 20
21................30
31................40
41................50
etc..
91...............100

但是我真的沒有一個解決方案如何增加第一個數組..:S

這是到目前為止我得到的:

package uebung05;

public class MatrixAddition
{
    public static void main(String[] argv)
    {
        int firstArray[][]  = new int[10][10];
        int secondArray[][] = new int[10][10];
        int ergArray[][]    = new int[10][10];

        System.out.println("Matrix 1\n----------------------------");

        // Inkrementieren der ersten Matrix
        for(int row = 0; row < firstArray.length; row++)
        {
            for(int column = 0; column < firstArray[row].length; column++)
            {
                // Increment Array here???
                System.out.print(firstArray[row][column] + "  ");
            }
            System.out.println();
        }

        System.out.println("\nMatrix 2\n----------------------------");

        // Dekrementieren der zweiten Matrix
        for(int row = 0; row < secondArray.length; row++)
        {
            for(int column = 0; column < secondArray[row].length; column++)
            {
                // Array mit Werten befüllen
                secondArray[row][column] = column + 1;
                System.out.print(secondArray[row][column] + "  ");
            }
            System.out.println();
        }

        System.out.println("\nAddition beider Matrizen\n----------------------------");

        // Addition firstArray & secondArray
        for(int row = 0; row < ergArray.length; row++)
        {
            for(int column = 0; column < ergArray[row].length; column++)
            {
                // Addition
                ergArray[row][column] = firstArray[row][column] +
                                        secondArray[row][column];

                System.out.print(ergArray[row][column] + "  ");
            }
            System.out.println();
        }
    }
}

將第一矩陣和第二矩陣相加的方法:

public static int[][] matrixAdd(int[][] A, int[][] B)
{
    // Check if matrices have contents
    if ((A.length < 0) || (A[0].length < 0)) return B;
    if ((B.length < 0) || (B[0].length < 0)) return A;

    // create new matrix to store added values in
    int[][] C = new int[A.length][A[0].length];

    for (int i = 0; i < A.length; i++)
    {
        for (int j = 0; j < A[i].length; j++)
        {
            C[i][j] = A[i][j] + B[i][j];
        }
    }
    return C;
}

但是我並沒有真正解決如何增加第一個數組的解決方案。

// Inkrementieren der ersten Matrix
        for(int row = 0; row < firstArray.length; row++)
        {
            for(int column = 0; column < firstArray[row].length; column++)
            {
                firstArray[row][column] = 1+ row*10 + column; 
                System.out.print(firstArray[row][column] + "  ");
            }
            System.out.println();
        }

將新的兩個矩陣求和並返回:

public int[][] addMatrixes(int[][] src1, int[][] src2){
  int[][] dst = new int[src1.length][src1[0].length];
  for(int i=0;i<src1.length;i++){
    for(int j=0;j<src1[0].length;j++){
      dst[i][j] = src1[i][j] + src2[i][j];
    }
  }
  return dst;
}

不是很通用,但是您可以僅通過一個簡單的循環定義第一個矩陣:

    int dim = 10;
    int size = dim*dim;
    int firstArray[][]  = new int[dim][dim];
    int row, column;

    for (int index = 0; index < size; index++ ){
        row = index/dim;
        column = index%dim;
        firstArray[row][column]=row*10+column+1;

        System.out.print(String.valueOf(firstArray[row][column])+"\t");
        if (column == 9){ System.out.println("");}
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM