# Save access to vector elements in struct

**URL:** <https://discourse.julialang.org/t/save-access-to-vector-elements-in-struct/19655>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 15, 2019, 12:37pm UTC](https://discourse.julialang.org/t/save-access-to-vector-elements-in-struct/19655 "2019-01-15T12:37:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [January 15, 2019, 12:37pm UTC](https://discourse.julialang.org/t/save-access-to-vector-elements-in-struct/19655/1 "2019-01-15T12:37:19Z")

</div>

I’d like to have some values in an array:

```julia
v = SVector{4, Float64}(rand(4)...)

```

and I want to modify (add) each element in an iterative process. But the modification occurs “somewhere else”: right now, those 4 values are scattered across 2 instances of a type with two fields (2x2=4). When I need this vector (for HCubature integration) I go and collect the values from the fields in my types and build the vector. This is a bit expensive.

I could restructure the whole thing so the modification part would act on this `v` vector instead of my types, but that really doesn’t make any sense from a structural point of view.

Or… I could maybe save a reference to the elements of this vector in my types:

```julia
using StaticArrays
struct V
    i::SubArray{Float64,1,MArray{Tuple{4},Float64,1,4},Tuple{UnitRange{Int64}},true}
end
struct T
    v::MVector{4, Float64}
    x::V
end

v = MVector{4, Float64}(rand(4)...)
t = T(v, V(view(v, 1:1)))
t.x.i[] += 2

```

This way I’ll always have both: a fully built vector _and_ its modifiable elements where I need them.

Sounds good, or am I missing something?

---

<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:** [January 15, 2019, 12:48pm UTC](https://discourse.julialang.org/t/save-access-to-vector-elements-in-struct/19655/2 "2019-01-15T12:48:43Z")

</div>

> [@yakir12](#):
>
> I go and collect the values from the fields in my types and build the vector. This is a bit expensive.

This doesn’t sound expensive?

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [January 15, 2019, 12:53pm UTC](https://discourse.julialang.org/t/save-access-to-vector-elements-in-struct/19655/3 "2019-01-15T12:53:12Z")

</div>

The function hcubature integrates needs to build a vector (albeit an SVector) of those values. I thought that in the same lines as “avoid constructing arrays inside for loops”, I should try and avoid that too?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 15, 2019, 12:58pm UTC](https://discourse.julialang.org/t/save-access-to-vector-elements-in-struct/19655/4 "2019-01-15T12:58:58Z")

</div>

This sounds way more complicated than it needs to be, but perhaps I just don’t understand your application. Why not just use an `MVector` and mutate it? Or use an `SVector` and replace individual elements using `StaticArrays.setindex` ?

> [@yakir12](#):
>
> The function hcubature integrates needs to build a vector (albeit an SVector) of those values. I thought that in the same lines as “avoid constructing arrays inside for loops”, I should try and avoid that too?

No, SVectors of bitstypes are themselves bitstypes and therefore pretty much free, and constructing new ones inside a loop is generally fine.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [January 15, 2019, 1:03pm UTC](https://discourse.julialang.org/t/save-access-to-vector-elements-in-struct/19655/5 "2019-01-15T13:03:48Z")

</div>

> [@rdeits](#):
>
> This sounds way more complicated than it needs to be, but perhaps I just don’t understand your application. Why not just use an `MVector` and mutate it? Or use an `SVector` and replace individual elements using `StaticArrays.setindex` ?

Just to answer this: the application doesn’t act on the vector. It acts on my types. At the same time, I want to integrate some of the values inside my types. The integration needs to return a vector. Therefore I need to convert my structure with my values peppered around my structure to a contingent vector.

> [@rdeits](#):
>
> No, SVectors of bitstypes are themselves bitstypes and therefore pretty much free, and constructing new ones inside a loop is generally fine.

Well! Then there’s no point in me doing that then. I just thought that reallocating a vector each time was expensive (for each iteration hcubature calls the function it integrates).

---

<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:** [January 15, 2019, 4:40pm UTC](https://discourse.julialang.org/t/save-access-to-vector-elements-in-struct/19655/6 "2019-01-15T16:40:28Z")

</div>

> [@yakir12](#):
>
> I thought that in the same lines as “avoid constructing arrays inside for loops”, I should try and avoid that too?

That doesn’t apply to StaticArrays, tuples, and similar types.
