1 |
|
2 |
|
3 | # Function subset
|
4 |
|
5 | Get or set a subset of a matrix or string.
|
6 |
|
7 |
|
8 | ## Syntax
|
9 |
|
10 | ```js
|
11 | math.subset(value, index) // retrieve a subset
|
12 | math.subset(value, index, replacement [, defaultValue]) // replace a subset
|
13 | ```
|
14 |
|
15 | ### Parameters
|
16 |
|
17 | Parameter | Type | Description
|
18 | --------- | ---- | -----------
|
19 | `matrix` | Array | Matrix | string | An array, matrix, or string
|
20 | `index` | Index | An index containing ranges for each dimension
|
21 | `replacement` | * | An array, matrix, or scalar. If provided, the subset is replaced with replacement. If not provided, the subset is returned
|
22 | `defaultValue` | * | Default value, filled in on new entries when the matrix is resized. If not provided, math.matrix elements will be left undefined. Default value: undefined.
|
23 |
|
24 | ### Returns
|
25 |
|
26 | Type | Description
|
27 | ---- | -----------
|
28 | Array | Matrix | string | Either the retrieved subset or the updated matrix.
|
29 |
|
30 |
|
31 | ## Examples
|
32 |
|
33 | ```js
|
34 | // get a subset
|
35 | const d = [[1, 2], [3, 4]]
|
36 | math.subset(d, math.index(1, 0)) // returns 3
|
37 | math.subset(d, math.index([0, 1], 1)) // returns [[2], [4]]
|
38 |
|
39 | // replace a subset
|
40 | const e = []
|
41 | const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]]
|
42 | const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 6], [0, 7]]
|
43 | ```
|
44 |
|
45 |
|
46 | ## See also
|
47 |
|
48 | [size](size.md),
|
49 | [resize](resize.md),
|
50 | [squeeze](squeeze.md),
|
51 | [index](index.md)
|