getSubmatrixAs1D method
Returns the submatrix of matrix
defined by row1, row2, col1, col2
as a 1D array, a sequence of rows. The total array length is nrows * ncols,
where nrows = row2 - row1 + 1 and ncols = col2 - col1 + 1.
The indices are inclusive.
The result may have 0 elements, depending on input indices.
It is not relevant wheter rows1 is < or > row2, or col1 < or > col2.
See also Array1D.splitArray to make a 2D array from the result.
See also getSubmatrix.
Implementation
static Float64List getSubmatrixAs1D(
List<Float64List> matrix, int row1, int row2, int col1, int col2) {
// Enable for check
// if(row1 < 0 || row2 < 0 || col1 < 0 ||col2 < 0 || row1 > sizef1-1 || row2 > sizef1-1 ||
// col1 > size-1 || col2 > size-1)
// throw new Exception("CompressedData2D: Submatrix out of bounds (2)");
if (row1 == row2 && col1 == col2) {
return new Float64List.fromList([matrix[row1][col1]]); // single point
}
int firstRow = row1;
int endRow = row2;
if (row1 > row2) {
firstRow = row2;
endRow = row1;
}
int firstCol = col1;
int endCol = col2;
if (col1 > col2) {
firstCol = col2;
endCol = col1;
}
int nrows = (row2 - row1 + 1).abs();
int ncols = (col2 - col1 + 1).abs();
if (row1 == row2) {
Float64List row = Array2D.getRow(matrix, firstRow);
Float64List result = new Float64List(ncols);
for (int i = 0; i < ncols; i++) {
result[i] = row[firstCol + i];
}
return result;
}
if (col1 == col2) {
Float64List col = Array2D.getColumn(matrix, firstCol);
Float64List result = new Float64List(nrows);
for (int i = 0; i < nrows; i++) {
result[i] = col[firstRow + i];
}
return result;
}
Float64List submatrix = new Float64List(nrows * ncols);
Float64List rowdata;
int isub = 0;
for (int i = firstRow; i <= endRow; i++) {
rowdata = matrix[i];
for (int j = firstCol; j <= endCol; j++) {
submatrix[isub++] = rowdata[j];
}
}
return submatrix;
}