diagonal matrix--diag

 diagonal matrix--diag

how to create a diagonal matrix?

diag() func.


how to get  diagonal elements from a matrix?

diag() func.


[syntax][descr]

(1) M=diag(Vector V,int k=0)

will return a |V|*|V| diagonal matrix

|V| is length of V


When k=0, M is  |V|*|V| size, M[i,i]=v[i] , M[i,j]=0 , for 1<=i , j <=|V|  i!=j , where i and j is positive integer. 

i.e.  value of  elements in M which its index belongs diagonal is equal to value of elements in Vector V, otherwise is equal to 0.

translation:對角線索引的值為V的元素的值,非對角線索引的值為0


When k>0,  M is |V+k|*|V+k| size, M[i+k,i+k]=v[i] ,M[i,j]=0 for 1<=i , j <=|V|  i!=j , where i , j and k is positive integer. 

[extra tips] You can think about M=diag(V), then add a row and column at left and button which all element are equal to 0.

translation:你可以想M=diag(V),然後把所有為0的元素加到最左行(第一行)和最下列(最後一列)


When k<0,  M is |V+k|*|V+k| size, M[i,i]=v[i] ,M[i,j]=0 for 1<=i , j <=|V|  i!=j , where i and j is positive integer.  k is negative integer.

[extra tips] You can think about M=diag(V), then add a row and column at right and up which all element are equal to 0.

translation:你可以想M=diag(V),然後把所有為0的元素加到最右行(最後一行)和最上列(第一列)


(2) diag(matrix A,int k=0)

When k=0, it will return all diagonal elements of A.

When k>0, it will return all elements of M[i,k+i] for all i in 1<=i<=k

When k<0, it will return all elements of M[i+k,i] for all i in 1<=i<=k


more deatils on

official website:

Create diagonal matrix or get diagonal elements of matrix - MATLAB diag (mathworks.com) 



Comments

Popular posts from this blog

a convenient type to record lots of value in MatLab -- cell matrix

Anonymous Function in MatLab