trace
对角线元素之和
说明
示例
矩阵对角线之和
矩阵的迹属性
验证矩阵的迹的几个属性(基于舍入误差)。
创建两个矩阵。验证 。
A = magic(3); B = rand(3); trace(A+B)
ans = 17.4046
trace(A) + trace(B)
ans = 17.4046
验证 。
trace(A)
ans = 15
trace(A')
ans = 15
验证 。
trace(A'*B)
ans = 22.1103
trace(A*B')
ans = 22.1103
验证对于标量 ,满足 。
c = 5; trace(c*A)
ans = 75
c*trace(A)
ans = 75
验证迹等于特征值之和,即 。
trace(A)
ans = 15
sum(eig(A))
ans = 15.0000
输入参数
A
— 输入矩阵
方阵
输入矩阵,指定为方阵。A
可以是满矩阵或稀疏矩阵。
数据类型: single
| double
复数支持: 是
算法
trace
提取对角线元素,并使用命令 sum(diag(A))
将其相加。迹的值与矩阵特征值之和 sum(eig(A))
相同(基于舍入误差)。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
代码生成不支持对此函数使用稀疏矩阵输入。
GPU 代码生成
使用 GPU Coder™ 为 NVIDIA® GPU 生成 CUDA® 代码。
用法说明和限制:
代码生成不支持对此函数使用稀疏矩阵输入。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
GPU 数组
通过使用 Parallel Computing Toolbox™ 在图形处理单元 (GPU) 上运行来加快代码执行。
此函数完全支持 GPU 数组。有关详细信息,请参阅Run MATLAB Functions on a GPU (Parallel Computing Toolbox)。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2006a 之前推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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)