diagind
diagind(M[, k])
A Range
giving the indices of the k
th diagonal of the matrix M
.
Examples
In the Julia programming language, the function diagind(M[, k])
is used to generate a range of indices corresponding to the kth diagonal of a matrix M
.
-
Get the main diagonal indices:
julia> M = [1 2 3; 4 5 6; 7 8 9]; julia> diagind(M) 3-element UnitRange{Int64}: 1:4:9
This example returns the indices
[1, 5, 9]
, which represent the main diagonal elements of the matrixM
. -
Get the first upper diagonal indices:
julia> M = [1 2 3; 4 5 6; 7 8 9]; julia> diagind(M, 1) 2-element UnitRange{Int64}: 2:4:8
It returns the indices
[2, 6]
, which represent the elements of the first upper diagonal of the matrixM
. - Get the second lower diagonal indices:
julia> M = [1 2 3; 4 5 6; 7 8 9]; julia> diagind(M, -2) 1-element UnitRange{Int64}: 7:8
It returns the indices
[7]
, which represent the elements of the second lower diagonal of the matrixM
.
Common mistake example:
julia> M = [1 2 3; 4 5 6; 7 8 9];
julia> diagind(M, 5)
ERROR: ArgumentError: k=5 is out of range. Expected -2:k_max=2.
In this example, the value of k
is out of the valid range for the given matrix M
. It's important to ensure that the value of k
is within the valid range specified by -2
to k_max
(maximum diagonal index) for the matrix.
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.