# Slicing a sparse variable created from list of tuples

**URL:** https://discourse.julialang.org/t/slicing-a-sparse-variable-created-from-list-of-tuples/94853
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [February 18, 2023, 9:14pm UTC](https://discourse.julialang.org/t/slicing-a-sparse-variable-created-from-list-of-tuples/94853 "2023-02-18T21:14:28Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![felipecordera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipecordera/32/43701_2.png) [@felipecordera](https://discourse.julialang.org/u/felipecordera)
#### Post date: [February 18, 2023, 9:14pm UTC](https://discourse.julialang.org/t/slicing-a-sparse-variable-created-from-list-of-tuples/94853/1 "2023-02-18T21:14:28Z")

</div>

Hi everyone,

I would like to do slicing on a sparse variable created from a list of tuples.

In the next example (extracted from documentation), the same variable is created in two different ways. While it is possible to do slicing on x1, it is not on x2.

```julia
N = 10
S = [(1, 1, 1), (N, N, N)]

model = Model(Gurobi.Optimizer)

@variable(model, x1[i=1:N, j=1:N, k=1:N; (i, j, k) in S])
@variable(model, x2[S])

x1[1,:,:], x2[1,:,:]

```

I get MethodError: no method matching one(::Colon)

Is there any way to create a sparse variable from a list of tuples and afterward be able to do slicing?

Thanks!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [February 19, 2023, 9:16pm UTC](https://discourse.julialang.org/t/slicing-a-sparse-variable-created-from-list-of-tuples/94853/2 "2023-02-19T21:16:20Z")

</div>

> [@felipecordera](#):
>
> Is there any way to create a sparse variable from a list of tuples and afterward be able to do slicing?

No. The key is to look at what is actually created:

```julia
julia> using JuMP

julia> N = 10
10

julia> S = [(1, 1, 1), (N, N, N)]
2-element Vector{Tuple{Int64, Int64, Int64}}:
 (1, 1, 1)
 (10, 10, 10)

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x1[i=1:N, j=1:N, k=1:N; (i, j, k) in S])
JuMP.Containers.SparseAxisArray{VariableRef, 3, Tuple{Int64, Int64, Int64}} with 2 entries:
  [1, 1, 1] = x1[1,1,1]
  [10, 10, 10] = x1[10,10,10]

julia> @variable(model, x2[S])
1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:
    Dimension 1, [(1, 1, 1), (10, 10, 10)]
And data, a 2-element Vector{VariableRef}:
 x2[(1, 1, 1)]
 x2[(10, 10, 10)]

```

- `x1` is a `JuMP.Containers.SparseAxisArray`, with three dimensions and two non-zero keys.
- `x2` is a `JuMP.Containers.DenseAxisArray`, with one dimension containing `S`.

So you can’t even index into `x2` with three arguments:

```julia
julia> x2[1, 1, 1]
ERROR: KeyError: key 1 not found

```

What you could do is something like this:

```julia
julia> x2[[s for s in S if s[1] == 1]]
1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:
    Dimension 1, [(1, 1, 1)]
And data, a 1-element Vector{VariableRef}:
 x2[(1, 1, 1)]

```
