subArray2d method

Array2d subArray2d(
  1. int rowStart,
  2. int rowEnd,
  3. int colStart,
  4. int colEnd,
)

Return a Sub Array2d with the rage of rows columns elements specified

  • rowStart row start index.
  • rowEnd row end index.
  • colStart column start index.
  • colEnd column start index.
  • FormatException: rowStart and rowEnd must be in Array2d.

Examples

var matrix = Array2d.empty();
var line = Array([1.0, 2.0, 3.0]);

matrix.add(line);
matrix.add(line);
matrix.add(line);

print(matrix.subArray2d(0, 1, 1, 2));

/* output:
Array2d([
  Array([2.0, 3.0]),
  Array([2.0, 3.0])
])
*/

Implementation

Array2d subArray2d(int rowStart, int rowEnd, int colStart, int colEnd) {
  if (rowStart > rowEnd || rowEnd >= l.length) {
    throw FormatException('rowStart and rowEnd must be in Array2d');
  }

  var c = Array2d.empty();
  for (var i = rowStart; i <= rowEnd; i++) {
    var row = this[i];

    if (colStart > colEnd || colEnd >= row.length) {
      throw FormatException('colStart and colEnd must be in Array2d');
    }

    var rowTemp = Array.empty();
    for (var j = colStart; j <= colEnd; j++) {
      rowTemp.add(this[i][j]);
    }

    c.add(rowTemp);
  }

  return c;
}