matrix reduce

For filters.

Reduce all elements of a matrix to a single value.

Command Type

built-in

Signature

> matrix reduce {flags} (closure)

Flags

  • --fold, -f {any}: The initial value for the accumulator

Parameters

  • closure: Reducing function. Arguments are the current element, then the accumulator (same order as reduce).

Input/output types:

input output
matrix any

Examples

Sum all elements of a 2x2 matrix

> [[1 2] [3 4]] | into matrix | matrix reduce --fold 0.0 {|e, acc| $acc + $e}
10.0

Product of all elements in a matrix

> [[1 2] [3 4]] | into matrix | matrix reduce --fold 1.0 {|e, acc| $acc * $e}
24.0

Sum without an initial value (uses first element as starting accumulator)

> [[1 2] [3 4]] | into matrix | matrix reduce {|e, acc| $acc + $e}
10.0

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.