slice

slice#

Slice an operator with new start and end points, and optionally new strides. Slice can also be used to drop ranks, for operations such as selecting a single row. Negative indices can be used to indicate starting at the end and going backward.

When slicing along any given tensor dimension, the start index is treated as inclusive, and the end index as exclusive.

auto matx::slice(const OpType &op, const index_t (&starts)[OpType::Rank()], const index_t (&ends)[OpType::Rank()], const index_t (&strides)[OpType::Rank()])#
auto matx::slice(const OpType &op, const index_t (&starts)[OpType::Rank()], const index_t (&ends)[OpType::Rank()])#

Examples#

auto t2 = make_tensor<TestType>({20, 10});
auto t3 = make_tensor<TestType>({30, 20, 10});
auto t4 = make_tensor<TestType>({40, 30, 20, 10});

(t2 = linspace<1>(t2.Shape(), (inner_type)0, (inner_type)10)).run(exec);
(t3 = linspace<2>(t3.Shape(), (inner_type)0, (inner_type)10)).run(exec);
(t4 = linspace<3>(t4.Shape(), (inner_type)0, (inner_type)10)).run(exec);
exec.sync();

// Slice with different start and end points in each dimension
auto t2t = slice(t2, {1, 2}, {3, 5});
auto t3t = slice(t3, {1, 2, 3}, {3, 5, 7});
auto t4t = slice(t4, {1, 2, 3, 4}, {3, 5, 7, 9});
auto t1 = make_tensor<TestType>({10});

t1.SetVals({10, 20, 30, 40, 50, 60, 70, 80, 90, 100});

// Slice every other element from a 1D tensor (stride of two)
auto t1t = slice(t1, {0}, {matxEnd}, {2});
// Slice "t2t" by selecting column "j" from a 2D operator and converting to a 1D operator
auto t2sly = slice<1>(t2t, {0, j}, {matxEnd, matxDropDim});
auto t2sn = slice(t2, {-4, -5}, {matxEnd, matxEnd});
auto t2s = slice(t2, {t2.Size(0) - 4, t2.Size(1) - 5}, {matxEnd, matxEnd});