# TL;DR tips for Julia users coming from Matlab

**URL:** https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093
**Category:** General Usage
**Created:** [June 9, 2020, 8:28pm UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093 "2020-06-09T20:28:08Z")
**Posts on this page:** 9
**Page:** 2

<div class="post-metadata">

### Author: ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)
#### Post date: [June 10, 2020, 10:40am UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/22 "2020-06-10T10:40:38Z")

</div>

This is amazing! I’m going to present to my lab in a couple weeks and this will be a great resource for everyone else.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 10, 2020, 11:36am UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/23 "2020-06-10T11:36:32Z")

</div>

> [@jlchan](#):
>
> The example `x = f(x)` by itself might be oversimplified. I see type instability when redefining variable names using anonymous functions

Interesting, the reason for this is [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276) and you can show it more succinctly with:

```julia
 function not_type_stable(a)
    a = a # toggle comment on this line
    return (x->x+a)(1)
end

```

So while I guess it is true that in some cases, assignments to a variable can cause performance problems, the reason isn’t really that `a` has one type and then you assign it to another type.

---

<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: [June 10, 2020, 12:19pm UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/24 "2020-06-10T12:19:16Z")

</div>

> [@roble](#):
>
> Another difference is, that any Matlab vector is just a row or column matrix.

This is one of those habits that many appear to be blind towards. New users are so often initializing arrays as

```julia
zeros(n, 1)
rand(n, 1)
# etc.

```

when they almost always should use

```julia
zeros(n)
rand(n)
# etc.

```

They’re not even going to think about it unless it’s pointed out directly. It’s a semi-subconscious assumption about how vectors work.

Similarly, use `rand()` instead of `rand(1)` or `rand(1)[1]`.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [June 10, 2020, 12:34pm UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/25 "2020-06-10T12:34:16Z")

</div>

> [@ettersi](#):
>
> You cannot have an immutable container with changing lengths,

I hate to be pedantic\*, but

```julia
julia> struct Foo
           x::Vector
       end

julia> Base.length(f::Foo) = length(f.x)

julia> Base.push!(f::Foo, a) = push!(f.x, a)

julia> foo=Foo([])
Foo(Any[])

julia> length(foo)
0

julia> push!(foo,1)
1-element Array{Any,1}:
 1

julia> length(foo)
1

```

\*That’s not true. I really enjoy bring pedantic 🤪. You’re off course correct.

---

<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: [June 10, 2020, 12:54pm UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/26 "2020-06-10T12:54:16Z")

</div>

Totally correct! Here’s another example of the same:

```julia
julia> struct Bar end

julia> Base.length(::Bar) = rand(0:1000)

julia> b = Bar()
Bar()

julia> length(b)
338

julia> length(b)
129

```

😉

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [June 10, 2020, 1:34pm UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/27 "2020-06-10T13:34:13Z")

</div>

```julia
linspace(a,b,n) = range(a; stop=b, length=n)   
logspace(a,b,n) = 10 .^ range(a; stop=b, length=n)

```

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [June 10, 2020, 7:53pm UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/28 "2020-06-10T19:53:27Z")

</div>

When I first transitioned from matlab to Julia, I set up atom snippets for some common functionality like this.  
That way I could type the old matlab stuff but get the new Julia stuff without any additional function definitions. Here are some

> <https://github.com/baggepinnen/configs/blob/4ea23b714c8c77ddd568c0fb91c02e8e52060159/scripts/snippets.cson#L127>

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 11, 2020, 6:34am UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/29 "2020-06-11T06:34:34Z")

</div>

> [@kevbonham](#):
>
> I hate to be pedantic

I love to be pedantic 😉 I am wondering if we should keep using the term “immutable” for these kinds of constructs in discussions. An “(immutable) `struct`” is a clearly defined concept in Julia, but `Foo` above may not mesh well with what various people understand as “immutable”.

Mutability (or lack thereof) in Julia is a very narrowly defined technical term that is meaningful mostly for composite types.

---

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [October 11, 2021, 4:44pm UTC](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093/30 "2021-10-11T16:44:40Z")

</div>

> [@jlchan](#):
>
> The manual is great, but I’ve found myself reading over parts without realizing that they applied to my current situations. I’ve found TL;DR explanations to be helpful in knowing where to look for information, not to replace more precise technical explanations.

Same here. An otherwise pleasant Julia experience can be brought to a longer halt if help is not accessible.

Also had this [tldr-pages](https://github.com/tldr-pages/tldr) or this [navi](https://www.reddit.com/r/bash/comments/q5wjj6/a_tool_that_autocompletes_your_bash_commands/) idea (in previous posts I called for one-liners or snippets).

We often ask posters to provide a minimum working example.  
I wondered if we could expect the same from someone answering a question, or contributes to Julia?  
Sounds ungrateful? Since it’s all free and based on voluntary work.  
You are all doing a great job - thanks!  
My only argument is that a help that is not understood is a lost effort.

Maybe you developers and helpers can go a bit further, think of the not-so-progressed, and add some TL;DR examples.

[Previous page](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093.md?page=1)
