# Bind variable to element of an array

**URL:** <https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 23, 2017, 11:19pm UTC](https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849 "2017-03-23T23:19:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [March 23, 2017, 11:19pm UTC](https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849/1 "2017-03-23T23:19:06Z")

</div>

I’m sure it is possible to bind a variable to an element of an array, but wasn’t able to figure out how, does anyone know?

---

<div class="post-metadata">

**Author:** ![ohsonice](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ohsonice](https://discourse.julialang.org/u/ohsonice)\
**Post date:** [March 23, 2017, 11:34pm UTC](https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849/2 "2017-03-23T23:34:47Z")

</div>

Check out the documentation for the @view macro.

Here’s an example (not sure if this is what you’re looking for or not):

> julia\> x = rand(2)  
> 2-element Array{Float64,1}:  
> 0.592149  
> 0.600268

> julia\> x1 = @view x[1]  
> 0-dimensional SubArray{Float64,0,Array{Float64,1},Tuple{Int64},false}:  
> 0.592149

> julia\> map!(x-\>x+1,x1)  
> 0-dimensional SubArray{Float64,0,Array{Float64,1},Tuple{Int64},false}:  
> 1.59215

> julia\> x1  
> 0-dimensional SubArray{Float64,0,Array{Float64,1},Tuple{Int64},false}:  
> 1.59215

> julia\> x  
> 2-element Array{Float64,1}:  
> 1.59215  
> 0.600268

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [March 24, 2017, 4:05pm UTC](https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849/3 "2017-03-24T16:05:13Z")

</div>

That’s pretty close. Is there a way to do this without having the number wrapped in a 0-dim array?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 24, 2017, 4:23pm UTC](https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849/4 "2017-03-24T16:23:59Z")

</div>

> [@jebej](#):
>
> Is there a way to do this without having the number wrapped in a 0-dim array?

I suppose you could define your own `Number` type that is a view. But otherwise, no: you need a mutable object here, and the built-in number types are immutable.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 24, 2017, 4:36pm UTC](https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849/5 "2017-03-24T16:36:29Z")

</div>

You don’t really need a mutable type but you do need a reference to the memory. A variable is not allowed to do that but it can be (efficiently) emulated. The closest to what you describe is `Ref`. You can get a reference to an array slot with `Ref(array, index)` which is also how you pass such a reference to C.

---

<div class="post-metadata">

**Author:** ![ohsonice](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ohsonice](https://discourse.julialang.org/u/ohsonice)\
**Post date:** [March 25, 2017, 5:23pm UTC](https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849/6 "2017-03-25T17:23:01Z")

</div>

Could you comment on best practices of mutating this reference? Here’s the best I could come up with:

> julia\> x = rand(2)  
> 2-element Array{Float64,1}:  
> 0.364822  
> 0.334024

> julia\> x1 = Ref(x,1)  
> Base.RefArray{Float64,Array{Float64,1},Void}([0.364822,0.334024],1,nothing)

> julia\> x1.x[x1.i] += 1  
> 1.3648224629409698

> julia\> x  
> 2-element Array{Float64,1}:  
> 1.36482  
> 0.334024

I could be misunderstanding the desired use case though, @jebej could you talk more to exactly what you want?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 25, 2017, 5:30pm UTC](https://discourse.julialang.org/t/bind-variable-to-element-of-an-array/2849/7 "2017-03-25T17:30:49Z")

</div>

`x1[]`
