rStats.js v1.3.0
    Preparing search index...

    Function diag

    • Matrix diagonals, diagonal matrices and identity matrices. Equivalent to R's diag(). It has three meanings: the diagonal of a matrix (as a vector); the diagonal matrix of a vector of length 2 or more (its values recycled along the diagonal of an nrow x ncol matrix when given); and for a single number n, the n x n identity matrix (with nrow/ncol, a matrix with that number on the diagonal).

      Parameters

      Returns number[] | number[][]

      diag([[1, 2], [3, 4]]); // [1, 4]
      diag([2, 3]); // [[2, 0], [0, 3]]
      diag(3); // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]