matrixFromFunction.md (1468B)
1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> 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 ### Throws 37 38 Type | Description 39 ---- | ----------- 40 41 42 ## Examples 43 44 ```js 45 math.matrixFromFunction([3,3], i => i[0] - i[1]) // an antisymmetric matrix 46 math.matrixFromFunction([100, 100], 'sparse', i => i[0] - i[1] === 1 ? 4 : 0) // a sparse subdiagonal matrix 47 math.matrixFromFunction([5], i => math.random()) // a random vector 48 ``` 49 50 51 ## See also 52 53 [matrix](matrix.md), 54 [zeros](zeros.md)