mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
72 lines
2.5 KiB
Markdown
72 lines
2.5 KiB
Markdown
---
|
|
layout: default
|
|
---
|
|
|
|
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
|
|
|
|
<h1 id="function-subset">Function subset <a href="#function-subset" title="Permalink">#</a></h1>
|
|
|
|
Get or set a subset of a matrix or string.
|
|
|
|
|
|
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
math.subset(value, index) // retrieve a subset
|
|
math.subset(value, index, replacement [, defaultValue]) // replace a subset
|
|
```
|
|
|
|
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
|
|
|
|
Parameter | Type | Description
|
|
--------- | ---- | -----------
|
|
`matrix` | Array | Matrix | string | An array, matrix, or string
|
|
`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.
|
|
`replacement` | * | An array, matrix, or scalar. If provided, the subset is replaced with replacement. If not provided, the subset is returned
|
|
`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.
|
|
|
|
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
Array | Matrix | string | Either the retrieved subset or the updated matrix.
|
|
|
|
|
|
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
|
|
|
|
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
// get a subset
|
|
const d = [[1, 2], [3, 4]]
|
|
math.subset(d, math.index(1, 0)) // returns 3
|
|
math.subset(d, math.index([0, 1], 1)) // returns [[2], [4]]
|
|
math.subset(d, math.index([false, true], 0)) // returns [[3]]
|
|
|
|
// replace a subset
|
|
const e = []
|
|
const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 0, 6]]
|
|
const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 0, 6], [0, 7, 0]]
|
|
math.subset(g, math.index([false, true], 1), 8) // returns [[5, 0, 6], [0, 8, 0]]
|
|
|
|
// get submatrix using ranges
|
|
const M = [
|
|
[1,2,3],
|
|
[4,5,6],
|
|
[7,8,9]
|
|
]
|
|
math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1, 2, 3], [4, 5, 6]]
|
|
```
|
|
|
|
|
|
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
|
|
|
|
[size](size.html),
|
|
[resize](resize.html),
|
|
[squeeze](squeeze.html),
|
|
[index](index.html)
|