# Accessing values in a matrix for a column before the first?

**URL:** <https://discourse.julialang.org/t/accessing-values-in-a-matrix-for-a-column-before-the-first/59634>\
**Category:** General Usage\
**Tags:** question, array\
**Created:** [April 20, 2021, 3:12am UTC](https://discourse.julialang.org/t/accessing-values-in-a-matrix-for-a-column-before-the-first/59634 "2021-04-20T03:12:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [April 20, 2021, 3:12am UTC](https://discourse.julialang.org/t/accessing-values-in-a-matrix-for-a-column-before-the-first/59634/1 "2021-04-20T03:12:34Z")

</div>

I am trying to access elements of the previous columns than first column.

For example in **python** if i try to access elements before the first column `0` it will return an `empty array`.

```python
import numpy as np
x = np.matrix([[1, 2, 10],
 [1, 4, 2],
 [2, 3, 2],
 [2, 3, 6]])
a = x[:, 0]
print('\n a is \n', a)
b = x[:, :0]
print('\n b is \n',b)

Output: -->

 a is 
 [[1]
 [1]
 [2]
 [2]]

 b is 
 []

```

But julia does not behave in such manner.

My trial in julia:

```julia
array = [1 2 3;3 4 5;2 3 4;1 2 3]
a = array[:, 1]
b = array[:, 1-1]

Output :-->

BoundsError: attempt to access 4×3 Array{Int64,2} at index [1:4, 0]

Stacktrace:
 [1] throw_boundserror(::Array{Int64,2}, ::Tuple{Base.Slice{Base.OneTo{Int64}},Int64}) at .\abstractarray.jl:541
 [2] checkbounds at .\abstractarray.jl:506 [inlined]
 [3] _getindex at .\multidimensional.jl:742 [inlined]
 [4] getindex(::Array{Int64,2}, ::Function, ::Int64) at .\abstractarray.jl:1060
 [5] top-level scope at In[1166]:1
 [6] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

However, to replicate such operation I tried to implement `try and catch` method and use it over the loop to see what happens when i try accessing different columns. The output is always an empty array.

```julia
for i in 1:3
    try
       b = array[:, :i-1] 
    catch 
        b = []
    end
    println("\b b in $i is $b")
end

Output :--->

 b in 1 is Any[]
 b in 2 is Any[]
 b in 3 is Any[]

```

May I know how can I access row elements from `first_column - 1` to `last_column - 1`, where `first_column - 1` returns an empty array but rest returns data from the matrix.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [April 20, 2021, 3:26am UTC](https://discourse.julialang.org/t/accessing-values-in-a-matrix-for-a-column-before-the-first/59634/2 "2021-04-20T03:26:59Z")

</div>

Is this what you want?

```julia
julia> array = [1 2 3;3 4 5;2 3 4;1 2 3]
4×3 Matrix{Int64}:
 1 2 3
 3 4 5
 2 3 4
 1 2 3

julia> array[:, 1:0]
4×0 Matrix{Int64}

```

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [April 20, 2021, 4:11am UTC](https://discourse.julialang.org/t/accessing-values-in-a-matrix-for-a-column-before-the-first/59634/3 "2021-04-20T04:11:00Z")

</div>

Thanks @ettersi , yes this is amazing.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 20, 2021, 10:30am UTC](https://discourse.julialang.org/t/accessing-values-in-a-matrix-for-a-column-before-the-first/59634/4 "2021-04-20T10:30:37Z")

</div>

@ettersi, the empty column output seems to be just the result of indexing with an empty array and not with a previous non-existing column index.  
Empty columns are thus produced by:

```julia
array = [1 2 3;3 4 5;2 3 4;1 2 3]

array[:, 1:0]
array[:, 2:1]
array[:, []]

# above empty indexing always produces:
4×0 Matrix{Int64}

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 20, 2021, 10:46am UTC](https://discourse.julialang.org/t/accessing-values-in-a-matrix-for-a-column-before-the-first/59634/5 "2021-04-20T10:46:10Z")

</div>

If understanding this correctly, the following might be closer to OP’s question?

```julia
array = [1 2 3;3 4 5;2 3 4;1 2 3]
ix = 1:size(array,2)

i = 2
array[:, (i-1 in ix) ? i-1 : []]
4-element Vector{Int64}:
 1
 3
 2
 1

i = 1
array[:, (i-1 in ix) ? i-1 : []]
4×0 Matrix{Int64}

```
