# For loop that references two different arrays

**URL:** <https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [September 10, 2018, 8:39pm UTC](https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782 "2018-09-10T20:39:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [September 10, 2018, 8:39pm UTC](https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782/1 "2018-09-10T20:39:17Z")

</div>

Hello,  
I am attempting to create an array “xminxhat” which references the values of two other arrays “xhat” and “yhat”. Essentially, I can’t figure out how to create a for loop that references two different arrays. I’ve shown my code below.

Thanks!  
Nakul

```julia
x = collect(0:99) #independent variable, sampled 100 times
y = rand(100)*100 #dependent variable, sampled 100 times
xhat = sum(x)/length(x)
yhat = sum(y)/length(y)
xminxhat = []

for a in xhat, b in xminxhat  
    xminxhat[a] = x[a] - xhat[a]
end

```

---

<div class="post-metadata">

**Author:** ![adamslc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adamslc/32/3452_2.png) [@adamslc](https://discourse.julialang.org/u/adamslc)\
**Post date:** [September 10, 2018, 8:47pm UTC](https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782/2 "2018-09-10T20:47:26Z")

</div>

You are looking for the the `zip` function, but I think that your code has some other issues as well. First, `xhat` is a scalar value, so it doesn’t make sense to index into it as `xhat[a]`, and second, `xminxhat` is a zero element vector, so you can’t index into that either!

I suspect that this is what you were going for, but my statistics are too rusty to be sure:

```julia
x = collect(0:99)
xhat = sum(x)/length(x)
xminxhat = []

for i in x  
    push!(xminxhat, i - xhat)
end

```

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [September 10, 2018, 8:58pm UTC](https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782/3 "2018-09-10T20:58:03Z")

</div>

Ah, I totally forgot that xhat is a scalar. push! really comes to the rescue for indexing into zero element vectors.

The code works great, thank you!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 11, 2018, 6:59pm UTC](https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782/6 "2018-09-11T18:59:51Z")

</div>

You don’t really need `push!` here, it’s quite slow, and you already know the final vector length. These three lines do the same but are more concise and about 4X faster.

```julia
x = 0:99
xhat = mean(x)
xminxhat = collect(x .- xhat)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 11, 2018, 8:30pm UTC](https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782/7 "2018-09-11T20:30:09Z")

</div>

> [@Seif\_Shebl](#):
>
> ```julia
> x = 0:99
> xhat = mean(x)
> xminxhat = collect(x .- xhat)
> 
> ```

Do you even need to collect the last vector? This is more than 3x faster again:

```julia
x = 0:99
xhat = mean(x)
xminxhat = x .- xhat

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 11, 2018, 8:43pm UTC](https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782/8 "2018-09-11T20:43:40Z")

</div>

> [@DNF](#):
>
> This is more than 3x faster again

Sure, it is, I just assumed that the OP needed to see the results at that moment, a range will not be very useful for this.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 11, 2018, 8:54pm UTC](https://discourse.julialang.org/t/for-loop-that-references-two-different-arrays/14782/9 "2018-09-11T20:54:04Z")

</div>

OK, but people seem to always be `collect`ing things needlessly, so I think it makes sense to question that impulse.
