trace
Sum of diagonal elements
Syntax
Description
Examples
Sum of Matrix Diagonal
Create a 3-by-3 matrix and calculate the sum of the diagonal elements.
A = [1 -5 2; -3 7 9; 4 -1 6]; b = trace(A)
b = 14
The result agrees with a manual calculation.
Matrix Trace Properties
Verify several properties of the trace of a matrix (up to round-off error).
Create two matrices. Verify that .
A = magic(3); B = rand(3); trace(A+B)
ans = 17.4046
trace(A) + trace(B)
ans = 17.4046
Verify that .
trace(A)
ans = 15
trace(A')
ans = 15
Verify that .
trace(A'*B)
ans = 22.1103
trace(A*B')
ans = 22.1103
Verify that for a scalar .
c = 5; trace(c*A)
ans = 75
c*trace(A)
ans = 75
Verify that the trace equals the sum of the eigenvalues .
trace(A)
ans = 15
sum(eig(A))
ans = 15.0000
Input Arguments
A
— Input matrix
square matrix
Input matrix, specified as a square matrix. A
can be full or
sparse.
Data Types: single
| double
Complex Number Support: Yes
Algorithms
trace
extracts the diagonal elements and adds them together with the
command sum(diag(A))
. The value of the trace is the same (up to round-off
error) as the sum of the matrix eigenvalues sum(eig(A))
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The trace
function
fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray
(Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)