Matrix subtraction

From , 5 Years ago, written in Java, viewed 232 times.
URL https://pastebin.vip/view/dbe272ba
  1.         /**
  2.          * 矩阵减法运算。 矩阵A和B可减的条件是矩阵A的行数等于矩阵B的行数,A的列数等于B的列数 c[i][j] = a[i][j] + b[i][j]
  3.          *
  4.          * @param b
  5.          *            减数
  6.          * @return
  7.          */
  8.         public Matrix sub(Matrix b) {
  9.                 if (b == null) {
  10.                         return null;
  11.                 }
  12.  
  13.                 Matrix c = null;
  14.                 double[][] bData = b.getMatrixData();
  15.                 if ((this.matrixData.length != bData.length)
  16.                                 || (this.matrixData[0].length != bData[0].length)) {
  17.                         System.out.println("两个矩阵的大小不一致,不能完成减法运算");
  18.                         return c;
  19.                 }
  20.                 // 结果矩阵的数据
  21.                 int cRow = this.matrixData.length;
  22.                 int cColumn = this.matrixData[0].length;
  23.                 double[][] cData = new double[cRow][cColumn];
  24.                 for (int i = 0; i < cRow; i++) {
  25.                         for (int j = 0; j < cColumn; j++) {
  26.                                 // 两矩阵对应位置的数字做减法
  27.                                 cData[i][j] = this.matrixData[i][j] - bData[i][j];
  28.                         }
  29.                 }
  30.                 c = new Matrix(cData);
  31.                 return c;
  32.         }

Reply to "Matrix subtraction"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website