2024-08-26 13:59:54 +02:00

74 lines
2.3 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-map">Function map <a href="#function-map" title="Permalink">#</a></h1>
Create a new matrix or array with the results of a callback function executed on
each entry of a given matrix/array.
For each entry of the input,
the callback is invoked with 2N + 1 arguments:
the N values of the entry, the index at which that entry occurs, and the N full
broadcasted matrix/array being traversed where N is the number of matrices being traversed.
Note that because the matrix/array might be
multidimensional, the "index" argument is always an array of numbers giving
the index in each dimension. This is true even for vectors: the "index"
argument is an array of length 1, rather than simply a number.
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.map(x, callback)
math.map(x, y, ..., callback)
```
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`x` | Matrix &#124; Array | The input to iterate on.
`callback` | Function | The function to call (as described above) on each entry of the input
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
Type | Description
---- | -----------
Matrix &#124; array | Transformed map of x; always has the same type and shape as x
<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.map([1, 2, 3], function(value) {
return value * value
}) // returns [1, 4, 9]
math.map([1, 2], [3, 4], function(a, b) {
return a + b
}) // returns [4, 6]
// The callback is normally called with three arguments:
// callback(value, index, Array)
// If you want to call with only one argument, use:
math.map([1, 2, 3], x => math.format(x)) // returns ['1', '2', '3']
// It can also be called with 2N + 1 arguments: for N arrays
// callback(value1, value2, index, BroadcastedArray1, BroadcastedArray2)
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[filter](filter.html),
[forEach](forEach.html),
[sort](sort.html)