Upper triangle of a matrix, overwriting M in the process.


  1. Modify a matrix by keeping only the upper triangle:

    julia> A = [1 2 3; 4 5 6; 7 8 9];
    julia> triu!(A)
    3×3 Array{Int64,2}:
    1  2  3
    0  5  6
    0  0  9

    This example modifies the matrix A by keeping only the upper triangle elements.

  2. Specify the superdiagonal to start from:

    julia> B = [1 2 3; 4 5 6; 7 8 9];
    julia> triu!(B, 1)
    3×3 Array{Int64,2}:
    0  2  3
    0  0  6
    0  0  0

    Here, the k value is set to 1, which means the upper triangle starts from the first superdiagonal.

  3. Handle edge cases with empty matrices:
    julia> C = Int64[]
    julia> triu!(C)
    0-element Array{Int64,1}

    It correctly handles the case when the input matrix is empty.

Common mistake example:

julia> D = [1 2; 3 4; 5 6];
julia> triu!(D, 5)
ERROR: BoundsError: attempt to access 3×2 Matrix{Int64} at index [3, 4]

In this example, the value of k provided is greater than the number of superdiagonals in the matrix D. It's important to ensure that k is within a valid range to avoid such errors. Always check the dimensions of the matrix before using triu!.

See Also

Ac_ldiv_B, Ac_ldiv_Bc, Ac_mul_B, Ac_mul_Bc, Ac_rdiv_B, Ac_rdiv_Bc, At_ldiv_B, At_ldiv_Bt, At_mul_B, At_mul_Bt, At_rdiv_B, At_rdiv_Bt, A_ldiv_Bc, A_ldiv_Bt, A_mul_B!, A_mul_Bc, A_mul_Bt, A_rdiv_Bc, A_rdiv_Bt, Bidiagonal, cond, conv2, det, diag, diagind, diagm, diff, eig, eigvals, eigvecs, expm, eye, full, inv, isdiag, ishermitian, isposdef, isposdef!, issym, istril, istriu, logabsdet, logdet, lyap, norm, qrfact, rank, repmat, rot180, rotl90, rotr90, sortrows, sqrtm, SymTridiagonal, trace, Tridiagonal, tril, tril!, triu, triu!, writedlm,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: