applyToRows method
Applies the given function func to each row in the matrix and returns a new matrix with the results.
func should be a function that takes a list argument (representing a row) and returns a list.
The returned list can be of different length from the input list, which allows this method to be used for operations like filtering or extending rows.
Example:
var matrix = Matrix.fromList([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
// Doubles the value of each element in each row
var doubledMatrix = matrix.applyToRows((row) => row.map((x) => x * 2).toList());
print(doubledMatrix);
// Output:
// [[2, 4, 6],
// [8, 10, 12],
// [14, 16, 18]]
// Filters each row to only keep even numbers
var evenMatrix = matrix.applyToRows((row) => row.where((x) => x % 2 == 0).toList());
print(evenMatrix);
// Output:
// [[2],
// [4, 6],
// [8]]
Note: The number of columns in the resulting matrix can vary from row to row if func changes the length of the rows.
Implementation
Matrix applyToRows(List<dynamic> Function(List<dynamic>) func) {
return Matrix(_data.map((row) => func(row)).toList());
}