# Create vector with dependent values

**URL:** https://discourse.julialang.org/t/create-vector-with-dependent-values/80611
**Category:** New to Julia
**Tags:** vector
**Created:** [May 6, 2022, 1:41pm UTC](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611 "2022-05-06T13:41:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)
#### Post date: [May 6, 2022, 1:41pm UTC](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611/1 "2022-05-06T13:41:05Z")

</div>

Hello,

I am new to Julia and I have to create a vector which values are dependent. Let me put an example:

If I have `MyStruct(22, [0, 0, 0], 0) `, the next values can be `MyStruct(21, [0, 0, 1], 0) `, `MyStruct(21, [0, 1, 0], 0) `, `MyStruct(21, [1, 0, 0], 0) `, `MyStruct(20, [0, 1, 1], 0) `, `MyStruct(20, [1, 0, 1], 0) `… That is `b::SVector{3, Int64}` increases as `a::Int ` decreases.

I tried to solve it by doing:

```julia
struct MyStruct
    a::Int 
    b::SVector{3, Int64} 
    c::Int 
end 

V1 = [MyStruct(a,(b),0) for a in 0:15 for b in [Iterators.product(ntuple(_ -> 0:10, 3)...)...]] #Terminal states remaining time 0

```

But with this I get impossible things as `MyStruct(22, [10, 10, 10], 0) `.

Can you help me? Thanks 🙂

---

<div class="post-metadata">

### Author: ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)
#### Post date: [May 6, 2022, 2:27pm UTC](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611/3 "2022-05-06T14:27:24Z")

</div>

Hello @rafael.guerra,

Consider that from `a::Int` you _moove_ to `b::SVector{3, Int64} `. That’s what I mean with:

> [@vamp](#):
>
> If I have `MyStruct(22, [0, 0, 0], 0) ` , the next values can be `MyStruct(21, [0, 0, 1], 0) ` , `MyStruct(21, [0, 1, 0], 0) ` , `MyStruct(21, [1, 0, 0], 0) ` , `MyStruct(20, [0, 1, 1], 0) ` , `MyStruct(20, [1, 0, 1], 0) ` … That is `b::SVector{3, Int64}` increases as `a::Int ` decreases.

In your example they are increasing.

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [May 6, 2022, 3:36pm UTC](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611/5 "2022-05-06T15:36:09Z")

</div>

If I understand your goal correctly, you almost had it. Something like this?

```julia
julia> mystructgenerator(amin, amax) = 
  ( MyStruct(a, b, 0) for a in amax:-1:amin
      for b in Iterators.product(0:amax-a,0:amax-a,0:amax-a)
        if a+sum(b)==amax
  )
mystructgenerator (generic function with 1 method)

julia> gen = mystructgenerator(18, 20)
Base.Iterators.Flatten{Base.Generator{StepRange{Int64, Int64}, var"#39#40"{Int64}}}(Base.Generator{StepRange{Int64, Int64}, var"#39#40"{Int64}}(var"#39#40"{Int64}(20), 20:-1:18))

julia> collect(gen)
10-element Vector{MyStruct}:
 MyStruct(20, [0, 0, 0], 0)
 MyStruct(19, [1, 0, 0], 0)
 MyStruct(19, [0, 1, 0], 0)
 MyStruct(19, [0, 0, 1], 0)
 MyStruct(18, [2, 0, 0], 0)
 MyStruct(18, [1, 1, 0], 0)
 MyStruct(18, [0, 2, 0], 0)
 MyStruct(18, [1, 0, 1], 0)
 MyStruct(18, [0, 1, 1], 0)
 MyStruct(18, [0, 0, 2], 0)

```

I tend to agree with this comment [Sum elements of a struct - #8 by stevengj](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/8), that you may not be using the right data structure for your problem. It’s hard to tell, because you haven’t explained what it is you are ultimately trying to achieve. You have been asking a few questions around `MyStruct` and how to extend various methods for it. Yet, I for one fail to see where you are going with this. The operations you are defining for this type seem quite artificial to me.

Anyway, hope this helps and please feel free to add any details.

---

<div class="post-metadata">

### Author: ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)
#### Post date: [May 6, 2022, 5:19pm UTC](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611/6 "2022-05-06T17:19:00Z")

</div>

Hello and thank you so much for your help, it works perfectly but I have realized that I was wrong. In your example:

```julia
 MyStruct(20, [0, 0, 0], 0)
 MyStruct(19, [1, 0, 0], 0)
 MyStruct(19, [0, 1, 0], 0)
 MyStruct(19, [0, 0, 1], 0)
 MyStruct(18, [2, 0, 0], 0)
 MyStruct(18, [1, 1, 0], 0)
 MyStruct(18, [0, 2, 0], 0)
 MyStruct(18, [1, 0, 1], 0)
 MyStruct(18, [0, 1, 1], 0)
 MyStruct(18, [0, 0, 2], 0)
.........

```

The sum of `a` and the 3 elements in `b` can`t be greater than the first value of `a`. I mean:

```julia
 MyStruct(18, [0, 0, 0], 0)
 MyStruct(18, [0, 0, 1], 0)
 MyStruct(18, [0, 1, 0], 0)
 MyStruct(18, [1, 0, 0], 0)
 MyStruct(18, [0, 1, 1], 0)
 MyStruct(18, [1, 0, 1], 0)
 MyStruct(18, [1, 1, 0], 0)
 MyStruct(18, [0, 0, 2], 0)
.........
```

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [May 7, 2022, 12:16pm UTC](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611/7 "2022-05-07T12:16:57Z")

</div>

You only need to alter the `if` statement in the generator expression slightly to make that happen.
