subset.md (1865B)
1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> 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 | For each dimension of the target, specifies an index or a list of indices to fetch or set. `subset` uses the cartesian product of the indices specified in 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 ### Throws 32 33 Type | Description 34 ---- | ----------- 35 36 37 ## Examples 38 39 ```js 40 // get a subset 41 const d = [[1, 2], [3, 4]] 42 math.subset(d, math.index(1, 0)) // returns 3 43 math.subset(d, math.index([0, 1], 1)) // returns [[2], [4]] 44 45 // replace a subset 46 const e = [] 47 const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]] and e = [[5, 0, 6]] 48 const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 6], [0, 7]] 49 50 // get submatrix using ranges 51 const M = [ 52 [1,2,3], 53 [4,5,6], 54 [7,8,9] 55 ] 56 math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1,2,3],[4,5,6]] 57 ``` 58 59 60 ## See also 61 62 [size](size.md), 63 [resize](resize.md), 64 [squeeze](squeeze.md), 65 [index](index.md)