matrixFromFunction

matrixFromFunction(size, fn)

matrixFromFunction(size, fn, format)

matrixFromFunction(size, fn, format, datatype)

matrixFromFunction(size, format, fn)

matrixFromFunction(size, format, datatype, fn)

Try it yourself:

See also:

Generate matrix entries with matrixFromFunction

matrixFromFunction creates a matrix by evaluating a callback for each row and column index. It is a concise way to define patterned matrices without manually typing every entry.

Useful patterns

Create a multiplication table, a coordinate grid, or a diagonal condition based on row and column indices. For an identity pattern, identity is simpler; zeros and ones cover uniform matrices.

Inputs and validation

Dimensions should be non-negative integers and the callback must return values appropriate for every requested index. Confirm the output with size, and use map to transform an already-existing collection instead of generating a new one.

All functions