# Help on loop function

**URL:** <https://discourse.julialang.org/t/help-on-loop-function/40260>\
**Category:** New to Julia\
**Tags:** question, loops\
**Created:** [May 27, 2020, 3:06pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260 "2020-05-27T15:06:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yasser\_rajwani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yasser_rajwani/32/14278_2.png) [@yasser\_rajwani](https://discourse.julialang.org/u/yasser_rajwani)\
**Post date:** [May 27, 2020, 3:06pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/1 "2020-05-27T15:06:35Z")

</div>

I have a code written

‘’’  
Y1 = (Data[1:15,10].\* ((1 .- Data[1:15,4]) + (0.3 .\* Data[1:15,4])))

‘’’

I now need the Y1 to go Y2, so on to Y10.

‘’’  
Y2 = (Data[1:15,11].\* ((1 .- Data[1:15,5]) + (0.3 .\* Data[1:15,5])))

‘’’  
As you can observe, some columns need to move increase by 1 (which I am hoping to code using the loop function)

I would like this to go on till Y10, with the columns changing (increase by 1)

Apologies, if this is difficult to understand.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 27, 2020, 3:12pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/2 "2020-05-27T15:12:18Z")

</div>

```julia
Y = zeros(15,10)
for i = 1:10
    Y[:,i] = (Data[1:15,9+i].* ((1 .- Data[1:15,3+i]) + (0.3 .* Data[1:15,3+i])))
end

```

Hint: Use triple backticks `to typeset code. On my keyboard, you can find the backtick` character in the top left corner right below esc, but your keyboard may be different.

---

<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:** [May 27, 2020, 4:56pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/3 "2020-05-27T16:56:17Z")

</div>

Remember to dot the + and the assignment = too.

Also, can’t you simplify the expression like this?

```julia
Y[:,i] .= Data[1:15,9+i] .* (1 .- 0.7.* Data[1:15,3+i])

```

For efficiency, you could also consider using `view`s.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 27, 2020, 5:15pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/4 "2020-05-27T17:15:20Z")

</div>

Do you really need to dot the `=` and the `-`? Dotting everything feels excessive.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 27, 2020, 5:20pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/5 "2020-05-27T17:20:58Z")

</div>

You can add `@.` at the beginning of the expression and then you won’t need to do it more.

But you will learn to appreciate the dot syntax. It makes things clearer compared to matlab.

```julia
for i = 1:10
           Y[:,i] = @. (Data[1:15,9+i] * ((1 - Data[1:15,3+i]) + (0.3 * Data[1:15,3+i])))
       end

```

---

<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:** [May 27, 2020, 9:24pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/6 "2020-05-27T21:24:56Z")

</div>

> [@Nathan\_Boyer](#):
>
> Do you really need to dot the `=` and the `-` ? Dotting everything feels excessive.

Yes, you do, or you can accept loss of performance and increased allocations. Alternatively, use `@.`.

The point of dotting everything is that it all fuses into a single loop with no unnecessary intermediate arrays. If you drop it a single place, intermediate arrays are needed to hold each separate calculation, before combining them at the end.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 28, 2020, 1:43pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/7 "2020-05-28T13:43:07Z")

</div>

> [@pdeffebach](#):
>
> ```julia
> for i = 1:10
> Y[:,i] = @. (Data[1:15,9+i] * ((1 - Data[1:15,3+i]) + (0.3 * Data[1:15,3+i])))
> end
> 
> ```

What is the practical difference here between placing `@.` after `=` rather than at the beginning of the line?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 28, 2020, 1:46pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/8 "2020-05-28T13:46:29Z")

</div>

Placing it after allocates a new array, and then stores the array.  
Placing it before writes directly into `Y[:,i]`.

Placing before is better.

---

<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:** [May 28, 2020, 2:36pm UTC](https://discourse.julialang.org/t/help-on-loop-function/40260/9 "2020-05-28T14:36:31Z")

</div>

The `@.` macro puts dots on all operators and function calls in the expression _after_ the macro. It cannot put dots on things _before_ the macro.
