trace#
Calculate the trace of a matrix: the sum of the elements on the main diagonal of a square matrix.
Available in: math cells, REPL.
Syntax#
trace(A)
Arguments and behavior#
For matrices, dimensions must match the operation. Singular matrices or incompatible shapes can fail. Cell indexing starts at 1. The example returns scalar (number).
Example#
Edit the inputs, then use Run or Reset. Changes stay in this example.
Example
A = [1, 2, 3; -1, 2, 3; 2, 0, 3]
trace(A)Interactive examples could not load. You can still copy the expressions.
Expected result: 6.
See also#
concat · det · diag · identity · inv · ones · range · size · squeeze · subset · transpose · zeros