matlab diag 函数
2016-05-05 14:45
911 查看
Create a 1-by-5 vector.
把向量生成矩阵:
v = [2 1 -1 -2 -5];
Use diag to create a matrix with the elements of v on the main diagonal.
D = diag(v)
D =
2 0 0 0 0
0 1 0 0 0
0 0 -1 0 0
0 0 0 -2 0
0 0 0 0 -5
Create a matrix with the elements of v on the first super diagonal (k=1).
D1 = diag(v,1)
D1 =
0 2 0 0 0 0
0 0 1 0 0 0
0 0 0 -1 0 0
0 0 0 0 -2 0
0 0 0 0 0 -5
0 0 0 0 0 0
The result is a 6-by-6 matrix. When you specify a vector of length n as an input, diag returns a square matrix of size n+abs(k).
对矩阵取对角线的值生成一个向量:
A = randi(10,6)
A =
9 3 10 8 7 8
10 6 5 10 8 1
2 10 9 7 8 3
10 10 2 1 4 1
7 2 5 9 7 1
1 10 10 10 2 9
x = diag(A)
x =
9
6
9
1
7
9
Get the elements on the first subdiagonal (k=-1) of A. The result has one fewer element than the main diagonal.
x1 = diag(A,-1)
x1 =
10
10
2
9
2
Calling diag twice returns a diagonal matrix composed of the diagonal elements of the original matrix.
来自MATLAB help
把向量生成矩阵:
v = [2 1 -1 -2 -5];
Use diag to create a matrix with the elements of v on the main diagonal.
D = diag(v)
D =
2 0 0 0 0
0 1 0 0 0
0 0 -1 0 0
0 0 0 -2 0
0 0 0 0 -5
Create a matrix with the elements of v on the first super diagonal (k=1).
D1 = diag(v,1)
D1 =
0 2 0 0 0 0
0 0 1 0 0 0
0 0 0 -1 0 0
0 0 0 0 -2 0
0 0 0 0 0 -5
0 0 0 0 0 0
The result is a 6-by-6 matrix. When you specify a vector of length n as an input, diag returns a square matrix of size n+abs(k).
对矩阵取对角线的值生成一个向量:
A = randi(10,6)
A =
9 3 10 8 7 8
10 6 5 10 8 1
2 10 9 7 8 3
10 10 2 1 4 1
7 2 5 9 7 1
1 10 10 10 2 9
x = diag(A)
x =
9
6
9
1
7
9
Get the elements on the first subdiagonal (k=-1) of A. The result has one fewer element than the main diagonal.
x1 = diag(A,-1)
x1 =
10
10
2
9
2
Calling diag twice returns a diagonal matrix composed of the diagonal elements of the original matrix.
来自MATLAB help
相关文章推荐
- matlab bar函数
- 写论文第七天:MATLAB之movavg函数
- 写论文第六天:MATLAB之leadlag函数
- 雷达截面积(Radar Cross Section,RCS)(MATLAB部分仿真+Code)
- 最小二乘法和主成分分析的比较 matlab 儿子的papa
- matlab 实用 api
- matlab 随机函数的使用
- MATLAB基础操作符与数据格式显示
- MATLAB矩阵基础运算
- matlab-命令学习
- 欢迎使用CSDN-markdown编辑器
- 卷积和及采样定理
- Matlab与C/C++联合编程之Matlab以MEX方式调用C代码(五)完整过程加示
- matlab和C/C++混合编程--Mex (六)参数传递
- Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(四)
- Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(二)
- Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(三)
- [转]matlab的Virtual Reality(虚拟现实)工具箱的应用简介
- 主瓣、栅瓣和旁瓣(MATLAB源代码+解释)
- 关于图像调和熵的Matlab编程