UNPKG

1.42 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function matrixFromFunction
4
5Create a matrix by evaluating a generating function at each index.
6The simplest overload returns a multi-dimensional array as long as `size` is an array.
7Passing `size` as a Matrix or specifying a `format` will result in returning a Matrix.
8
9
10## Syntax
11
12```js
13math.matrixFromFunction(size, fn)
14math.matrixFromFunction(size, fn, format)
15math.matrixFromFunction(size, fn, format, datatype)
16math.matrixFromFunction(size, format, fn)
17math.matrixFromFunction(size, format, datatype, fn)
18```
19
20### Parameters
21
22Parameter | Type | Description
23--------- | ---- | -----------
24`size` | Array &#124; 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
31Type | Description
32---- | -----------
33Array &#124; Matrix | Returns the created matrix
34
35
36## Examples
37
38```js
39math.matrixFromFunction([3,3], i => i[0] - i[1]) // an antisymmetric matrix
40math.matrixFromFunction([100, 100], 'sparse', i => i[0] - i[1] === 1 ? 4 : 0) // a sparse subdiagonal matrix
41math.matrixFromFunction([5], i => math.random()) // a random vector
42```
43
44
45## See also
46
47[matrix](matrix.md),
48[zeros](zeros.md)