.. highlightlang:: us .. index:: diag .. _diag: diag ==== .. us.tag diag ENGLISH math-create-mat Changed5502 :ref:`diag` creates a diagonal-matrix or a row-vector. .. function:: r = diag(m) r = diag(m, k) .. us.return **Return Value** If *m* is a row- or column-vector, :ref:`diag` creates a matrix. If *m* is a *nr* * *nc* matrix, :ref:`diag` creates a row-vector with ``min([nr, nc])`` columns. .. us.params **Parameters** .. uparam:: m *m* is a real, complex or string vector or matrix. .. uparam:: k *k* is a real scalar and sets the of kth diagonal: k = 0 represents the main diagonal, k > 0 above the main diagonal, and k < 0 below the main diagonal. Default value is 0. .. us.example **Example** :: diag([1,2,3]) 1.0000 0.0000 0.0000 0.0000 2.0000 0.0000 0.0000 0.0000 3.0000 [1,2;3,4] 1.0000 2.0000 3.0000 4.0000 diag([1,2;3,4]) 1.0000 4.0000 diag([1,2,3], 2) 0.0000 0.0000 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000 2.0000 0.0000 0.0000 0.0000 0.0000 0.0000 3.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 .. us.history **History** .. list-table:: :header-rows: 1 * - Version - Description * - R2013.2 - New parameter *k*. .. seealso:: :ref:`overview-mathematic` :sub:`id-1146555`