www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CreateDiagonalMatricesExample.m

    %% Create Diagonal Matrices  

%% 
% 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)  

%% 
% Create a matrix with the elements of |v| on the first super diagonal
% (|k=1|).
D1 = diag(v,1) 

%%
% 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)|.