# Indexing 1-Dimentional Arrays

**URL:** <https://discourse.julialang.org/t/indexing-1-dimentional-arrays/30615>\
**Category:** General Usage\
**Tags:** question, array, type\
**Created:** [November 2, 2019, 11:23am UTC](https://discourse.julialang.org/t/indexing-1-dimentional-arrays/30615 "2019-11-02T11:23:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)\
**Post date:** [November 2, 2019, 11:23am UTC](https://discourse.julialang.org/t/indexing-1-dimentional-arrays/30615/1 "2019-11-02T11:23:03Z")

</div>

Hello.

I was reading Julia’s manual ([Section: Noteworthy differences](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-MATLAB-1)) and came across this quote.

> Julia has true one-dimensional arrays. Column vectors are of size `N` , not `Nx1` . For example, [`rand(N)`](https://docs.julialang.org/en/v1/stdlib/Random/#Base.rand) makes a 1-dimensional array.

It is true that

```julia
A = Array{Int8,1}(undef, 3)

B = Array{Int8,2}(undef, 3,1)

C = Array{Int8,2}(undef, 1,3)

```

are all different types of `Array`. But why is it possible to eval for example

```julia
julia> A[2,1]
-35

```

if A has 1 dimension only?

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [November 2, 2019, 11:36am UTC](https://discourse.julialang.org/t/indexing-1-dimentional-arrays/30615/2 "2019-11-02T11:36:01Z")

</div>

There is a convention that a scalar value can be used in the same way as an N-dimensional array, provided that it is always accessed at index 1:

```julia
julia> x = 42
42

julia> x[1]
42

julia> x[1, 1]
42

julia> x[1, 1, 1]
42

```

The above works, because all non-existing axes are accessed only at index 1. As soon as you try to access another index, you get an out-of-bounds error:

```julia
julia> x[2]
ERROR: BoundsError
Stacktrace:
 [1] getindex(::Int64, ::Int64) at ./number.jl:78
 [2] top-level scope at REPL[13]:1

julia> x[1, 2]
ERROR: BoundsError
Stacktrace:
 [1] getindex(::Int64, ::Int64, ::Int64) at ./number.jl:83
 [2] top-level scope at REPL[14]:1

```

  

In the same way, any N-dimensional array can be used as an M-dimensional array (with M\>N), provided that all extra axes are accessed at index 1. These all work:

```julia
julia> A = rand(10);

julia> A[2,1]
0.9346414306362032

julia> A[2,1,1]
0.9346414306362032

```

but the following does not:

```julia
julia> A[2,2]
ERROR: BoundsError: attempt to access 10-element Array{Float64,1} at index [2, 2]
Stacktrace:
 [1] getindex(::Array{Float64,1}, ::Int64, ::Int64) at ./array.jl:729
 [2] top-level scope at REPL[18]:1

```

  

This is documented with more details in

[https://docs.julialang.org/en/v1/manual/arrays/#Omitted-and-extra-indices-1](https://docs.julialang.org/en/v1/manual/arrays/#Omitted-and-extra-indices-1)

---

<div class="post-metadata">

**Author:** ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)\
**Post date:** [November 2, 2019, 12:08pm UTC](https://discourse.julialang.org/t/indexing-1-dimentional-arrays/30615/3 "2019-11-02T12:08:12Z")

</div>

Thanks for the explanation.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [November 2, 2019, 4:03pm UTC](https://discourse.julialang.org/t/indexing-1-dimentional-arrays/30615/4 "2019-11-02T16:03:29Z")

</div>

I was a little surprised recently that this works for zero-dimensional `Array`s but not for `Ref`:

```julia
julia> fill(0)[1] # zero-array
0

julia> fill(0)[1,1]
0

julia> Ref(0)[1] # ndims(Ref(0)) == 0 too
ERROR: MethodError: no method matching getindex(::Base.RefValue{Int64}, ::Int64)

julia> (0,)[1,1]
ERROR: MethodError: no method matching getindex(::Tuple{Int64}, ::Int64, ::Int64)

```

---

<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 3, 2019, 8:18am UTC](https://discourse.julialang.org/t/indexing-1-dimentional-arrays/30615/5 "2019-11-03T08:18:51Z")

</div>

Since `RefValue` is not an `AbstractArray`, there is no reason to expect that it would support the interface for the latter.
