1 |
|
2 |
|
3 | # Function matrixFromFunction
|
4 |
|
5 | Create a matrix by evaluating a generating function at each index.
|
6 | The simplest overload returns a multi-dimensional array as long as `size` is an array.
|
7 | Passing `size` as a Matrix or specifying a `format` will result in returning a Matrix.
|
8 |
|
9 |
|
10 | ## Syntax
|
11 |
|
12 | ```js
|
13 | math.matrixFromFunction(size, fn)
|
14 | math.matrixFromFunction(size, fn, format)
|
15 | math.matrixFromFunction(size, fn, format, datatype)
|
16 | math.matrixFromFunction(size, format, fn)
|
17 | math.matrixFromFunction(size, format, datatype, fn)
|
18 | ```
|
19 |
|
20 | ### Parameters
|
21 |
|
22 | Parameter | Type | Description
|
23 | --------- | ---- | -----------
|
24 | `size` | Array | Matrix | The size of the matrix to be created
|
25 | `fn` | function | Callback function invoked for every entry in the matrix
|
26 | `format` | string | The Matrix storage format, either `'dense'` or `'sparse'`
|
27 | `datatype` | string | Type of the values
|
28 |
|
29 | ### Returns
|
30 |
|
31 | Type | Description
|
32 | ---- | -----------
|
33 | Array | Matrix | Returns the created matrix
|
34 |
|
35 |
|
36 | ## Examples
|
37 |
|
38 | ```js
|
39 | math.matrixFromFunction([3,3], i => i[0] - i[1]) // an antisymmetric matrix
|
40 | math.matrixFromFunction([100, 100], 'sparse', i => i[0] - i[1] === 1 ? 4 : 0) // a sparse subdiagonal matrix
|
41 | math.matrixFromFunction([5], i => math.random()) // a random vector
|
42 | ```
|
43 |
|
44 |
|
45 | ## See also
|
46 |
|
47 | [matrix](matrix.md),
|
48 | [zeros](zeros.md)
|