# (Not) concatenating arrays

**URL:** <https://discourse.julialang.org/t/not-concatenating-arrays/40803>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [June 5, 2020, 10:43am UTC](https://discourse.julialang.org/t/not-concatenating-arrays/40803 "2020-06-05T10:43:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sparrowhawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sparrowhawk/32/9148_2.png) [@sparrowhawk](https://discourse.julialang.org/u/sparrowhawk)\
**Post date:** [June 5, 2020, 10:43am UTC](https://discourse.julialang.org/t/not-concatenating-arrays/40803/1 "2020-06-05T10:43:57Z")

</div>

I have a large `Vector{Float64}` called `x`, a few elements of which remain unchanged within a loop. I would like the remaining elements to reflect the values in another array `b`, before passing on to a function within the loop which does some heavy lifting. Something like this

```julia
    # x is preallocated outside the loop, 
    # changeidx remains the same every iteration
    # b changes on every iteration but is preallocated outside the loop 
    x[changeidx] .= b
    foo!(output, x)
    # output is also preallocated

```

Is there a way to send `foo!` the variable `x` without actually copying the values of `b` into `x`? Will a `view` help me here? Or is there a best practice for this kind of operation?

Thanks

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [June 5, 2020, 11:08am UTC](https://discourse.julialang.org/t/not-concatenating-arrays/40803/2 "2020-06-05T11:08:47Z")

</div>

You could use a view of x in combination with `Iterators.Flatten` if you only need to iterate through rather than index into the array.

---

<div class="post-metadata">

**Author:** ![sparrowhawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sparrowhawk/32/9148_2.png) [@sparrowhawk](https://discourse.julialang.org/u/sparrowhawk)\
**Post date:** [June 5, 2020, 1:23pm UTC](https://discourse.julialang.org/t/not-concatenating-arrays/40803/3 "2020-06-05T13:23:29Z")

</div>

That sounds like a good idea but I’m not sure I know enough about `Iterators.Flatten` to implement what you’re saying. Could you elaborate how I could send `x` with a mixture of `view` and `flatten` to `foo!` ?

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [June 5, 2020, 1:54pm UTC](https://discourse.julialang.org/t/not-concatenating-arrays/40803/4 "2020-06-05T13:54:21Z")

</div>

```julia
for value in Iterators.flatten((@view(x[unchangedidx]), b))
    # do something with value
end

```

In 1.4 and below, it’s probably worthwhile to do `xview = @view x[unchagedidx]` outside of your loop since it allocates, but `@view` no longer allocates in 1.5, so it won’t be necessary to do so for efficiency in the future.
