# Using generalized array indexes

**URL:** <https://discourse.julialang.org/t/using-generalized-array-indexes/556>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 25, 2016, 11:10am UTC](https://discourse.julialang.org/t/using-generalized-array-indexes/556 "2016-11-25T11:10:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 25, 2016, 11:10am UTC](https://discourse.julialang.org/t/using-generalized-array-indexes/556/1 "2016-11-25T11:10:43Z")

</div>

After reading through the PR below, and some blog posts about generalized indexing in `AbstractArrays`, I am wondering if the code below is the right way to iterate through the rows of a matrix:

```julia
"Check if argument is a probability vector."
isprobvec{T}(v::AbstractVector{T}) = all(x->x ≥ 0, v) && sum(v) ≈ one(T)

"Check if argument is a probability (stochastic) matrix."
isprobmat(m::AbstractMatrix) = all(i->isprobvec(view(m, i, :)), indices(m, 1))

```

> <https://github.com/JuliaLang/julia/pull/16260>
>
> This is the one part of \[ArrayIteration.jl\](https://github.com/timholy/ArrayIter…ation.jl) that I think it's reasonable to move to Base for the julia-0.5 release. The core change is the addition of the ~~\`inds\`~~ \`indices\` function, which should be thought of as a sister function to \`size\` but instead returns a tuple containing the in-bounds indexes of an array. The fallback is
> 
> \`\`\` jl
> indices(A::AbstractArray, d) = 1:size(A,d)
> indices{T,N}(A::AbstractArray{T,N}) = ntuple(d-\>inds(A, d), Val{N})
> \`\`\`
> 
> But this allows one to override \`indices\` for specific types:
> 
> \`\`\` jl
> A = OffsetArray(\[1 3; 2 4\], (-1,2))
> @test A\[0,3\] == 1
> @test A\[1,3\] == 2
> @test A\[0,4\] == 3
> @test A\[1,4\] == 4
> julia\> A\[1,1\]
> ERROR: BoundsError: attempt to access OAs.OffsetArray{Int64,2,Array{Int64,2}} with inds (0:1,3:4):
> #undef #undef
> #undef #undef
> at index \[1,1\]
> in throw\_boundserror(::OAs.OffsetArray{Int64,2,Array{Int64,2}}, ::Tuple{Int64,Int64}) at ./abstractarray.jl:134
> \[inlined code\] from ./abstractarray.jl:103
> in getindex(::OAs.OffsetArray{Int64,2,Array{Int64,2}}, ::Int64, ::Int64) at /home/tim/src/julia-0.5/test/abstractarray.jl:542
> in eval(::Module, ::Any) at ./boot.jl:230
> \`\`\`
> 
> This adds the definition and uses it in \`checkbounds\`. The bigger job will be wiring it into the rest of Base. So I thought I'd post at this stage and see what folks think.
