# Help with two Julia lines of code whose syntax is unclear to me

**URL:** <https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769>\
**Category:** New to Julia\
**Created:** [March 12, 2019, 6:49am UTC](https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769 "2019-03-12T06:49:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samq/32/47935_2.png) [@samq](https://discourse.julialang.org/u/samq)\
**Post date:** [March 12, 2019, 6:49am UTC](https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769/1 "2019-03-12T06:49:18Z")

</div>

While studying neural nets I came across 2 lines of code whose syntax baffle me.

I looked at the docs, but I still don’t get the 2 lines of code discussed below.

Each line is prefaced by code context.

-----------FIRST LINE-----------------------------

```julia
i = 5003 # simple
display(images[i]) # simple

```

#=  
Here is the first line. I already know that this line outputs  
a label value, largest Float64 in images data, and the prediction value associated with the largest Float64.

labels[i] are labels for data fed to neural network(NN);

images[i] are the data fed to the NN;

preprocess turns image data into Float64s;

model is the NN;

findmax returns the largest Float64.

What I don’t get is how the logic of this line works for the “.- (0, 1)” part; what is it doing and how?  
=#

```julia
labels[i], findmax(model(preprocess(images[i]))) .- (0, 1)

```

* * *

-----------SECOND LINE-----------------------------  
#= findmax, model, preprocess and images have already been defined above.

The following function definition returns largest Float64 and an index.

What I don’t get in this function definition is what the “[2]-1” part does. What is the logic behind it and how does it work?  
=#

```julia
prediction(i) = findmax(model(preprocess(images[i])))[2]-1

```

* * *

Thank you in advance for helping me understand Julia syntax better.

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [March 12, 2019, 6:58am UTC](https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769/2 "2019-03-12T06:58:45Z")

</div>

Findmax returns a tuple, as you can check:

```julia
julia> r = rand(10);

julia> findmax(r)
(0.7466769692672559, 7)

```

And `.-` is element-wise subtraction, and `[2]` takes the second element:

```julia
julia> findmax(r) .- (2,3)
(-1.253323030732744, 4)

julia> findmax(r)[2]
7

```

ps. It would be easier to read if you could [quote your code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530).

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [March 12, 2019, 7:05am UTC](https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769/3 "2019-03-12T07:05:51Z")

</div>

To further explain the motivation for the above code, `findmax` returns a tuple of (maximum value, index), and the `.- (0, 1)` part is used to subtract 1 from the index but leave the value unchanged, i.e. change the index from 1-based to 0-based:

```julia
julia> r = rand(3)
3-element Array{Float64,1}:
 0.43522862419490305
 0.7670653549580913
 0.4966565072349449

julia> findmax(r)
(0.7670653549580913, 2)

julia> findmax(r) .- (0, 1)
(0.7670653549580913, 1)

```

I agree with you that it’s not exactly obvious at first sight. A clearer way of writing the same thing would be:

```julia
julia> maxval, maxidx = findmax(r)
(0.7670653549580913, 2)

julia> (maxval, maxidx - 1)
(0.7670653549580913, 1)

```

In your second line, `[2]` chooses the index only, and `-1` changes it to be 0-based.

---

<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:** [March 12, 2019, 8:02am UTC](https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769/4 "2019-03-12T08:02:46Z")

</div>

Hi. It is easier to read and understand your questions if you take a look at and follow the advice here: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

In particular, it is helpful if you quote your code in triple backticks (as explained in that post), so it’s easier to read.

If you click on the edit button, you can update your post to fix the layout and font issues.

---

<div class="post-metadata">

**Author:** ![samq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samq/32/47935_2.png) [@samq](https://discourse.julialang.org/u/samq)\
**Post date:** [March 12, 2019, 5:51pm UTC](https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769/5 "2019-03-12T17:51:41Z")

</div>

@improbable22 Thank you Thank you Thank you!!  
I will definitely follow your suggestion and quote my code in the future.

In fact, I will edit this post right now so others can read it more easily.

---

<div class="post-metadata">

**Author:** ![samq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samq/32/47935_2.png) [@samq](https://discourse.julialang.org/u/samq)\
**Post date:** [March 12, 2019, 5:53pm UTC](https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769/6 "2019-03-12T17:53:03Z")

</div>

@bennedich Thank you so very much for such a wonderful and clear explanation! You are an immense help. Thank you again!

---

<div class="post-metadata">

**Author:** ![samq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samq/32/47935_2.png) [@samq](https://discourse.julialang.org/u/samq)\
**Post date:** [March 12, 2019, 6:12pm UTC](https://discourse.julialang.org/t/help-with-two-julia-lines-of-code-whose-syntax-is-unclear-to-me/21769/7 "2019-03-12T18:12:38Z")

</div>

@ DNF Thank you for your suggestion.

I editing the code in this post so others may read it better per you suggestion.

Thank you!
