Function matrixFromFunction #

Create a matrix by evaluating a generating function at each index. The simplest overload returns a multi-dimensional array as long as size is an array. Passing size as a Matrix or specifying a format will result in returning a Matrix.

Syntax #

math.matrixFromFunction(size, fn)
math.matrixFromFunction(size, fn, format)
math.matrixFromFunction(size, fn, format, datatype)
math.matrixFromFunction(size, format, fn)
math.matrixFromFunction(size, format, datatype, fn)

Parameters #

Parameter Type Description
size Array | Matrix The size of the matrix to be created
fn function Callback function invoked for every entry in the matrix
format string The Matrix storage format, either 'dense' or 'sparse'
datatype string Type of the values

Returns #

Type Description
Array | Matrix Returns the created matrix

Examples #

math.matrixFromFunction([3,3], i => i[0] - i[1]) // an antisymmetric matrix
math.matrixFromFunction([100, 100], 'sparse', i => i[0] - i[1] === 1 ? 4 : 0) // a sparse subdiagonal matrix
math.matrixFromFunction([5], i => math.random()) // a random vector

See also #

matrix, zeros

Fork me on GitHub