@@ -237,22 +237,14 @@ end
237237
238238fillstored! (A:: AbstractMatrix , v) = fill! (A, v)
239239
240- diagind (m:: Integer , n:: Integer , k:: Integer = 0 ) = diagind (IndexLinear (), m, n, k)
241- diagind (:: IndexLinear , m:: Integer , n:: Integer , k:: Integer = 0 ) =
242- k <= 0 ? range (1 - k, step= m+ 1 , length= min (m+ k, n)) : range (k* m+ 1 , step= m+ 1 , length= min (m, n- k))
243-
244- function diagind (:: IndexCartesian , m:: Integer , n:: Integer , k:: Integer = 0 )
245- Cstart = CartesianIndex (1 + max (0 ,- k), 1 + max (0 ,k))
246- Cstep = CartesianIndex (1 , 1 )
247- length = max (0 , k <= 0 ? min (m+ k, n) : min (m, n- k))
248- StepRangeLen (Cstart, Cstep, length)
249- end
250-
251240"""
252241 diagind(M::AbstractMatrix, k::Integer = 0, indstyle::IndexStyle = IndexLinear())
253242 diagind(M::AbstractMatrix, indstyle::IndexStyle = IndexLinear())
243+ diagind(::IndexStyle, m::Integer, n::Integer, k::Integer = 0)
244+ diagind(m::Integer, n::Integer, k::Integer = 0)
254245
255- An `AbstractRange` giving the indices of the `k`th diagonal of the matrix `M`.
246+ An `AbstractRange` giving the indices of the `k`th diagonal of a matrix,
247+ specified either by the matrix `M` itself or by its dimensions `m` and `n`.
256248Optionally, an index style may be specified which determines the type of the range returned.
257249If `indstyle isa IndexLinear` (default), this returns an `AbstractRange{Integer}`.
258250On the other hand, if `indstyle isa IndexCartesian`, this returns an `AbstractRange{CartesianIndex{2}}`.
@@ -262,6 +254,7 @@ If `k` is not provided, it is assumed to be `0` (corresponding to the main diago
262254See also: [`diag`](@ref), [`diagm`](@ref), [`Diagonal`](@ref).
263255
264256# Examples
257+ The matrix itself may be passed to `diagind`:
265258```jldoctest
266259julia> A = [1 2 3; 4 5 6; 7 8 9]
2672603×3 Matrix{Int64}:
@@ -276,6 +269,18 @@ julia> diagind(A, IndexCartesian())
276269StepRangeLen(CartesianIndex(1, 1), CartesianIndex(1, 1), 3)
277270```
278271
272+ Alternatively, dimensions `m` and `n` may be passed to get the diagonal of an `m×n` matrix:
273+ ```jldoctest
274+ julia> m, n = 5, 7
275+ (5, 7)
276+
277+ julia> diagind(m, n, 2)
278+ 11:6:35
279+
280+ julia> diagind(IndexCartesian(), m, n)
281+ StepRangeLen(CartesianIndex(1, 1), CartesianIndex(1, 1), 5)
282+ ```
283+
279284!!! compat "Julia 1.11"
280285 Specifying an `IndexStyle` requires at least Julia 1.11.
281286"""
286291
287292diagind (A:: AbstractMatrix , indexstyle:: IndexStyle ) = diagind (A, 0 , indexstyle)
288293
294+ function diagind (:: IndexCartesian , m:: Integer , n:: Integer , k:: Integer = 0 )
295+ Cstart = CartesianIndex (1 + max (0 ,- k), 1 + max (0 ,k))
296+ Cstep = CartesianIndex (1 , 1 )
297+ length = max (0 , k <= 0 ? min (m+ k, n) : min (m, n- k))
298+ StepRangeLen (Cstart, Cstep, length)
299+ end
300+
301+ diagind (:: IndexLinear , m:: Integer , n:: Integer , k:: Integer = 0 ) =
302+ k <= 0 ? range (1 - k, step= m+ 1 , length= min (m+ k, n)) : range (k* m+ 1 , step= m+ 1 , length= min (m, n- k))
303+ diagind (m:: Integer , n:: Integer , k:: Integer = 0 ) = diagind (IndexLinear (), m, n, k)
304+
289305"""
290306 diag(M, k::Integer=0)
291307
0 commit comments