# How can I change specific values in array?

**URL:** <https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349>\
**Category:** New to Julia\
**Tags:** question, array, function, struct, tables\
**Created:** [May 20, 2022, 6:28am UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349 "2022-05-20T06:28:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [May 20, 2022, 6:28am UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/1 "2022-05-20T06:28:30Z")

</div>

Hello,

I am new to Julia and I am using [POMDPs.jl package](https://github.com/JuliaPOMDP), specially [TabularTDLearning.jl](https://github.com/JuliaPOMDP/TabularTDLearning.jl/blob/3630ddfdb1bac2e95912fa9648e14d6fb6831563/src/q_learn.jl#L45).

[ValuePolicy](https://github.com/JuliaPOMDP/POMDPPolicies.jl/blob/6249800d9b21d5f322e8505caa14a8b331e7ad39/src/vector.jl#L53) has a property called `value_table` which you can access to get the q-values and can also be modified, for example for all zeros would be:

```julia
solver.Q_vals = zeros(length(states(mdp)), length(actions(mdp)))
solve(solver, mdp)

```

In this table, rows are the states and the collums are the actions.

In my case states and actions are structs:

```julia
struct State
    s1::Int 
    s2::SVector{2, Int} 
    s3::Int 
end

struct Action 
    a::SVector{2, Int} 
end

```

After, the whole state-space and the action-space is defined as follows:

```julia
states = SS, #State-space
actions = AS, #Action-space

```

I have create a function that given an state, returns all the possible actions:

```julia
function A(s::State)
    return [(Action(a)) for a in Iterators.product(0:12.-s.s2[1],0:12-s.s2[2]) if sum(a)<=s.s1]
end

```

My objetive is to go collum by collum on the `value_table` and for every action that is not possible set a value on the table of for example -9999.

I have tried many loops but it doesn`t work. What is the best way to get it? Maybe a function?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [May 20, 2022, 11:36am UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/2 "2022-05-20T11:36:24Z")

</div>

So there already seems to be some problems with you actions function where

```julia
function A(s::State)
    return [(Action(a)) for a in Iterators.product(0:12.-s.s2[1],0:12-s.s2[2]) if sum(a)<=s.s1]
end

```

does not work, and might not do what you expect. First it complains about `0:12.-s.s2[1]` since it is not clear if you want `0:12.0 - s.s2[1]` or `0:12 .- s.s2[1]`. Then the question is if you want to compute `0:(12 .- s.s2[1])` or `(0:12) .- s.s2[1]`, where it is the first one that is actually computed.

For the actual question I’m not quite sure exactly what you want. My understanding is you want to loop over all states and actions, and if an action is not valid for a certain state you want to update some table with a large negative value? If that is it you could probably do it somewhat like this, though it might not be the most efficient

```julia
for s in states(mdp)
    valid_actions = A(s)
    for a in actions(mdp)
        if !(a in valid_actions)
            solver.Q_vals[state2idx(s), act2idx(a)] = -9999
        end
    end
end

```

and here I assume that you have some functions that transform state and action to an index in the table.

---

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [May 20, 2022, 1:26pm UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/3 "2022-05-20T13:26:51Z")

</div>

Hello,

This is fixed:

> [@albheim](#):
>
> does not work, and might not do what you expect. First it complains about `0:12.-s.s2[1]` since it is not clear if you want `0:12.0 - s.s2[1]` or `0:12 .- s.s2[1]` . Then the question is if you want to compute `0:(12 .- s.s2[1])` or `(0:12) .- s.s2[1]` , where it is the first one that is actually computed.

> [@albheim](#):
>
> For the actual question I’m not quite sure exactly what you want. My understanding is you want to loop over all states and actions, and if an action is not valid for a certain state you want to update some table with a large negative value? If that is it you could probably do it somewhat like this, though it might not be the most efficient

Yes, exactly.

> [@albheim](#):
>
> and here I assume that you have some functions that transform state and action to an index in the table.

When I try your code I get:

```julia
UndefVarError: state2idx not defined

```

I have look for [this](https://juliapomdp.github.io/POMDPs.jl/latest/api/#POMDPs.stateindex) but it does not work. I got:

```julia

ERROR: MethodError: no method matching setindex!(::Nothing, ::Int64, ::Int64, ::Int64)

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [May 20, 2022, 1:40pm UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/4 "2022-05-20T13:40:44Z")

</div>

That was what i meant with assuming you had a function doing the conversion, you somehow have to select a mapping (or maybe that exists in pomps package) to say what state/action gets mapped to what index.

---

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [May 20, 2022, 1:49pm UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/5 "2022-05-20T13:49:05Z")

</div>

Yes, I found [this](https://juliapomdp.github.io/POMDPs.jl/latest/api/#POMDPs.stateindex). And did:

```julia
for s in states(mdp)
    valid_actions = A(s)
    for a in actions(mdp)
        if !(a in valid_actions)
            solver.Q_vals[POMDPs.stateindex(mdp, s) , POMDPs.actionindex(mdp, a)] = -9999
        end
    end
end

```

But error was:

```julia
ERROR: MethodError: no method matching setindex!(::Nothing, ::Int64, ::Int64, ::Int64)

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [May 20, 2022, 1:53pm UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/6 "2022-05-20T13:53:02Z")

</div>

That seems to say that `solver.Q_vals` is nothing. Are you sure that this is the field you are supposed to use? On the phone now so too lazy to try to look it up 😅

---

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [May 20, 2022, 1:56pm UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/7 "2022-05-20T13:56:06Z")

</div>

I am not really sure, but I found [this](https://github.com/JuliaPOMDP/TabularTDLearning.jl/blob/3630ddfdb1bac2e95912fa9648e14d6fb6831563/src/q_learn.jl#L33) that might be the reason. `Q_vals::Union{Nothing, Matrix{Float64}} = nothing`

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [May 20, 2022, 2:26pm UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/8 "2022-05-20T14:26:33Z")

</div>

It does look like that would be the case.

Maybe just try to initialize it like you showed in the first post?

---

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [May 20, 2022, 2:26pm UTC](https://discourse.julialang.org/t/how-can-i-change-specific-values-in-array/81349/9 "2022-05-20T14:26:56Z")

</div>

Solution:

```julia
solver.Q_vals = zeros(length(states(mdp)), length(actions(mdp)))

```

Before your code.

Thank you so much.
