slice
Syntax
slice(obj, index)
or
slice(obj, rowIndex, [colIndex])
which is equivalent to
obj[index]
or obj[rowIndex, colIndex]
Arguments
obj can be a matrix or a table.
index, rowIndex and colIndex can be scalar/vector/pair indicating the row or column index. If index, rowIndex or colIndex is a pair, it indicates the range of index which is left-closed and right-open.
Details
For slice(obj, index)
:
-
If obj is a matrix and
-
index is a scalar, it returns a vector indicating a column.
-
index is a vector or a pair, it returns a matrix of selected columns.
-
-
If obj is a table and
-
index is a scalar, it returns a dictionary indicating a row.
-
index is a vector or a pair, it returns a table of selected rows.
-
For slice(obj, rowIndex, [colIndex])
:
-
If obj is a matrix and
-
rowIndex and colIndex are both scalars, it returns a scalar indicating the value of specified element of the matrix.
-
rowIndex is a scalar and colIndex is a pair (or vise versa), it returns a submatrix of selected rows and columns.
-
rowIndex and colIndex are both vectors or pairs, it returns a submatrix of selected rows and columns.
-
-
If obj is a table and
-
rowIndex and colIndex are both scalars, return a scalar indicating the value of specified element of the table.
-
rowIndex is a scalar and colIndex is a pair (or vise versa), it returns a table of selected rows and columns.
-
rowIndex and colIndex are both vectors or pairs, it returns a table of selected rows and columns.
-
-
To get a particular row or column from a table, consider using function
col
orrow
. -
When index, rowIndex or colIndex specifies the index range of a matrix, if the values are not within [0, size(X)-1], the corresponding results are NULL values.
Examples
If obj is a matrix:
m=1..9$3:3
m.slice(0);
// output
[1,2,3]
m.slice([0]);
#0 |
---|
1 |
2 |
3 |
m.slice(0:2);
#0 | #1 |
---|---|
1 | 4 |
2 | 5 |
3 | 6 |
m.slice(0 2);
#0 | #1 |
---|---|
1 | 7 |
2 | 8 |
3 | 9 |
m.slice(0,1);
// output
4
m.slice(0 1,0 1);
#0 | #1 |
---|---|
1 | 4 |
2 | 5 |
m.slice(1:2,1:2);
#0 |
---|
5 |
If obj is a table:
t=table(`A`B`C as sym,2018.01.01..2018.01.03 as date,10 48 5 as val)
t.slice(0);
// output
val->10
date->2018.01.01
sym->A
t.slice([0]);
sym | date | val |
---|---|---|
A | 2018.01.01 | 10 |
t.slice(0 1);
sym | date | val |
---|---|---|
A | 2018.01.01 | 10 |
B | 2018.01.02 | 48 |
t.slice(0:1);
sym | date | val |
---|---|---|
A | 2018.01.01 | 10 |
t.slice(0,1);
// output
2018.01.01
t.slice(0 1,0 1);
sym | date |
---|---|
A | 2018.01.01 |
B | 2018.01.02 |
t.slice(1:2,1:2);
date |
---|
2018.01.02 |