2025-09-05 10:41:44 +02:00

77 lines
2.6 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-range">Function range <a href="#function-range" title="Permalink">#</a></h1>
Create a matrix or array containing a range of values.
By default, the range end is excluded. This can be customized by providing
an extra parameter `includeEnd`.
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.range(str [, includeEnd]) // Create a range from a string,
// where the string contains the
// start, optional step, and end,
// separated by a colon.
math.range(start, end [, includeEnd]) // Create a range with start and
// end and a step size of 1.
math.range(start, end, step [, includeEnd]) // Create a range with start, step,
// and end.
```
<h3 id="where">Where <a href="#where" title="Permalink">#</a></h3>
- `str: string`
A string 'start:end' or 'start:step:end'
- `start: {number | bigint | BigNumber | Fraction | Unit}`
Start of the range
- `end: number | bigint | BigNumber | Fraction | Unit`
End of the range, excluded by default, included when parameter includeEnd=true
- `step: number | bigint | BigNumber | Fraction | Unit`
Step size. Default value is 1.
- `includeEnd: boolean`
Option to specify whether to include the end or not. False by default.
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`args` | * | Parameters describing the range's `start`, `end`, and optional `step`.
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
Type | Description
---- | -----------
Array &#124; Matrix | range
<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
math.range(2, 6) // [2, 3, 4, 5]
math.range(2, -3, -1) // [2, 1, 0, -1, -2]
math.range('2:1:6') // [2, 3, 4, 5]
math.range(2, 6, true) // [2, 3, 4, 5, 6]
math.range(2, math.fraction(8,3), math.fraction(1,3)) // [fraction(2), fraction(7,3)]
math.range(math.unit(2, 'm'), math.unit(-3, 'm'), math.unit(-1, 'm')) // [2 m, 1 m, 0 m , -1 m, -2 m]
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[ones](ones.html),
[zeros](zeros.html),
[size](size.html),
[subset](subset.html)