array2dSubToScalar function

dynamic array2dSubToScalar(
  1. Array2d a,
  2. num b, {
  3. bool returnNewArray = false,
})

Subtract all the matrix elements for a number

Examples

var a = Array2d([
  Array([2, 2, 2]),
  Array([2, 2, 2]),
  Array([2, 2, 2])
]);

print(array2dSubToScalar(a, 2););

/* output:
Array2d([
  Array([0.0, 0.0, 0.0]),
  Array([0.0, 0.0, 0.0]),
  Array([0.0, 0.0, 0.0])
])
*/

Implementation

dynamic array2dSubToScalar(Array2d a, num b, {bool returnNewArray = false}) {
  if (returnNewArray) {
    var c = Array2d.fixed(a.row, a.column);
    a.asMap().forEach((i, row) {
      row.asMap().forEach((j, element) {
        c[i][j] = element - b;
      });
    });
    return c;
  } else {
    for (var i = 0; i < a.row; i++) {
      for (var j = 0; j < a.column; j++) {
        a[i][j] = a[i][j] - b;
      }
    }
  }
}