subVector method
Extracts a subVector from the given vector using the specified indices or range.
indices: Optional list of integers representing the indices to include in the subVector.range: Optional string representing the range (e.g. "1:3").start: Optional start index of the range.end: Optional end index of the range.
Returns a new vector containing the specified subVector.
Example 1:
var v = Vector.fromList([1, 2, 3, 4, 5]);
var subVector = v.subVector(indices: [0, 2, 4]);
print(subVector); // Output: "Vector3(1.0, 3.0, 5.0)"
Example 2:
var v = Vector.fromList([1, 2, 3, 4, 5]);
var subVector = v.subVector(range: '1:3');
print(subVector); // Output: "Vector3(2.0, 3.0, 4.0)"
Example 3:
var v = Vector.fromList([1, 2, 3, 4, 5]);
var subVector = v.subVector(start: 1, end: 3);
print(subVector); // Output: "Vector3(2.0, 3.0, 4.0)"
@throws Exception If the indices or range are out of the Vector's bounds.
Implementation
Vector subVector(
{List<int>? indices, String range = '', int? start, int? end}) {
final indices_ = indices ??
(start != null && end != null
? List.generate(end - start + 1, (i) => start + i)
: (range.isNotEmpty
? _Utils.parseRange(range, length)
: (start != null
? List.generate(length - start, (i) => start + i)
: (end != null
? List.generate(end + 1, (i) => i)
: List.generate(length, (i) => i)))));
if (indices_.any((i) => i < 0 || i >= _data.length)) {
throw Exception('Indices are out of range');
}
List newValues = indices_.map((i) => _data[i]).toList();
return Vector.fromList(newValues);
}