def setdiag(G, val):
    """
    Sets the diagonal elements of a matrix to a specified value.

    Parameters
    ----------
    G: A 2D matrix or array.
        The matrix to modify.

    val: Int or float
        The value to which the diagonal of 'G' will be set.
    """
    n = G.shape[0]
    for i in range(0,n):
        G[i,i] = val

    return G