STK++ 0.9.13
Arrays Tutorial 5 : Slicing arrays and expressions

This page explains the slicing operations.

A slice is a rectangular part of an array or expression. Arrays slices can be used both as rvalues and as lvalues while Expression slice can only be used as rvalue.

Rows and Columns

Individual columns and rows of arrays and expressions can be accessed using STK::ExprBase::col() and STK::ExprBase::row() as rvalue and using STK::ArrayBase::col() and STK::ArrayBase::row() methods as lvalue. The argument for col() and row() is the index of the column or row to be accessed.

ExampleOutput
#include "STKpp.h"
using namespace STK;
int main(int argc, char *argv[])
{
CArrayXX A(4, 6); CArray2X B(2,4, -1.);
A << 1, 2, 3, 4, 1, 2,
4, 3, 1, 3, 2, 4,
1, 3, 4, 2, 1, 4,
2, 3, 1, 4, 3, 2;
stk_cout << _T("A =\n") << A;
stk_cout << _T("B =\n") << B;
A.sub(B.rows(), B.cols()) = B; // copy B in A(0:1,0:3)
stk_cout << _T("A =\n") << A;
A.row(3,B.cols()) = meanByCol(B); // copy a row-vector in row 3 of A
stk_cout << _T("A =\n") << A;
}
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
This file include all the header files of the STK++ project.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
The namespace STK is the main domain space of the Statistical ToolKit project.
int main(int argc, char **argv)
A =
1 2 3 4 1 2
4 3 1 3 2 4
1 3 4 2 1 4
2 3 1 4 3 2
B =
-1 -1 -1 -1
-1 -1 -1 -1
A =
-1 -1 -1 -1 1 2
-1 -1 -1 -1 2 4
 1  3  4  2 1 4
 2  3  1  4 3 2
A =
-1 -1 -1 -1 1 2
-1 -1 -1 -1 2 4
 1  3  4  2 1 4
-1 -1 -1 -1 3 2

Sub-Arrays and sub-Vectors

The most general sub operation is called sub(). There are two versions, one for arrays, an other for vectors. The sub-Arrays and sub-vectors can be employed as rvalue or lvalue, meaning you can assign or reference a sub-array or sub-vector.

ExampleOutput
#include "STKpp.h"
using namespace STK;
int main(int argc, char** argv)
{
VectorX b(3, 0);
std::cout << "b=\n" << b;
b.sub(Range(2)) = 1.;
std::cout << "b=\n" << b << "\n";
ArrayXX a(3, 4); a << 1.,2.,3.,4.
, 1.,2.,3.,4.
, 1.,1.,1.,1.;
std::cout << "a=\n" << a;
a.col(1) = b;
a.row(1, Range(2,2)) = 0.;
a.sub(Range(1,2), Range(2,2)) = 9.;
std::cout << "a=\n" << a;
return 0;
}
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
b=
0
0
0
b=
1
1
0

a=
1 2 3 4
1 2 3 4
1 1 1 1
a=
1 1 3 4
1 1 9 9
1 0 9 9