| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Symbolic Math Toolbox |
| Contents | Index |
| Learn more about Symbolic Math Toolbox |
diag(A, k)
diag(A)
diag(A, k) returns a square symbolic matrix of order n + abs(k), with the elements of A on the k-th diagonal. A must present a row or column vector with n components. The value k = 0 signifies the main diagonal. The value k > 0 signifies the k-th diagonal above the main diagonal. The value k < 0 signifies the k-th diagonal below the main diagonal. If A is a square symbolic matrix, diag(A, k) returns a column vector formed from the elements of the k-th diagonal of A.
diag(A), where A is a vector with n components, returns an n-by-n diagonal matrix having A as its main diagonal. If A is a square symbolic matrix, diag(A) returns the main diagonal of A.
Create a symbolic matrix with the main diagonal presented by the elements of the vector v:
syms a b c; v = [a b c]; diag(v)
The result is:
ans = [ a, 0, 0] [ 0, b, 0] [ 0, 0, c]
Create a symbolic matrix with the second diagonal below the main one presented by the elements of the vector v:
syms a b c; v = [a b c]; diag(v, -2)
The result is:
ans = [ 0, 0, 0, 0, 0] [ 0, 0, 0, 0, 0] [ a, 0, 0, 0, 0] [ 0, b, 0, 0, 0] [ 0, 0, c, 0, 0]
Extract the main diagonal from a square matrix:
syms a b c x y z; A = [a, b, c; 1, 2, 3; x, y, z]; diag(A)
The result is
ans = a 2 z
Extract the first diagonal above the main one:
syms a b c x y z; A = [a, b, c; 1, 2, 3; x, y, z]; diag(A, 1)
The result is:
ans = b 3
![]() | det | diff | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |