- Main Page
- Tutorials
- Functions
- Release Notes
- Examples

Functions
diag
Complete List of ArrayFire Functions ArrayFire Functions by Category » Functions to create and modify Arrays » | Functions to create arrays.

Extract diagonal from a matrix when `extract` is set to true. More...

## Functions
AFAPI array | diag (const array &in, const int num=0, const bool extract=true)
AFAPI af_err | af_diag_create (af_array *out, const af_array in, const int num)
AFAPI af_err | af_diag_extract (af_array *out, const af_array in, const int num)

## Detailed Description

Extract diagonal from a matrix when `extract` is set to true.

Create a diagonal marix from input array when `extract` is set to false

// Extraction
array a = randu(dim4(4, 3));
// a [4 3 1 1]
// 0.0000 0.5328 0.6793
// 0.1315 0.2190 0.9347
// 0.7556 0.0470 0.3835
// 0.4587 0.6789 0.5194
array b = diag(a);
// b [3 1 1 1]
// 0.0000
// 0.2190
// 0.3835
array c = diag(a, 1);
// c [2 1 1 1]
// 0.5328
// 0.9347
// Creation
array a = randu(4);
// a [4 1 1 1]
// 0.0000
// 0.1315
// 0.7556
// 0.4587
array b = diag(a, 0, false);
// b [4 4 1 1]
// 0.0000 0.0000 0.0000 0.0000
// 0.0000 0.1315 0.0000 0.0000
// 0.0000 0.0000 0.7556 0.0000
// 0.0000 0.0000 0.0000 0.4587
array b = diag(a, -1, false);
// c [5 5 1 1]
// 0.0000 0.0000 0.0000 0.0000 0.0000
// 0.0000 0.0000 0.0000 0.0000 0.0000
// 0.0000 0.1315 0.0000 0.0000 0.0000
// 0.0000 0.0000 0.7556 0.0000 0.0000
// 0.0000 0.0000 0.0000 0.4587 0.0000

## Function Documentation

AFAPI af_err af_diag_create | ( | af_array * | out,
|  | const af_array | in,
|  | const int | num
| ) |  |

Parameters

  [out] | out | is the array created from the input array `in`
  [in] | in | is the input array which is the diagonal
  [in] | num | is the diagonal index

AFAPI af_err af_diag_extract | ( | af_array * | out,
|  | const af_array | in,
|  | const int | num
| ) |  |

Parameters

  [out] | out | is the `num` -th diagonal of `in`
  [in] | in | is the input matrix
  [in] | num | is the diagonal index

AFAPI array af::diag | ( | const array & | in,
|  | const int | num = `0`,
|  | const bool | extract = `true`
| ) |  |

Parameters

  [in] | in | is the input array
  [in] | num | is the diagonal index
  [in] | extract | when true returns an array containing diagonal of tha matrix and when false returns a matrix with `in` as diagonal

Returns
  an array with either the diagonal or the matrix based on `extract`

Examples:
  svd.cpp.