# Conversion from Matlab to Julia

**URL:** https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317
**Category:** New to Julia
**Tags:** matlab
**Created:** [August 13, 2021, 4:15am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317 "2021-08-13T04:15:00Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [August 20, 2021, 2:20pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/21 "2021-08-20T14:20:47Z")

</div>

> [@soldin](#):
>
> I hope in terms of accuracy Julia should be as good as MATLAB, or at least not too far behind. That would make me switch to Julia for DEs as wel

That’s a strong statement, we mean for ODEs? If so, please provide such example and I am sure this will be resolved.

---

<div class="post-metadata">

### Author: ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)
#### Post date: [August 20, 2021, 2:32pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/22 "2021-08-20T14:32:09Z")

</div>

Oh, I think I didn’t write down my thoughts correctly. I only meant that I believe that Julia should be at par with MATLAB (or somewhere near), not that I know of some cases where it is not.

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [August 20, 2021, 2:32pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/23 "2021-08-20T14:32:57Z")

</div>

I do not read the statement as complaning but rather asking. Something like “I feel like switching fully to Julia but I worry if the accuracy of solvers for ODEs is as good as in Matlab. If yes, I will switch fully”.

---

<div class="post-metadata">

### Author: ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)
#### Post date: [August 20, 2021, 2:33pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/24 "2021-08-20T14:33:45Z")

</div>

Exactly, zdenek. 🙂

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [August 20, 2021, 2:52pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/25 "2021-08-20T14:52:09Z")

</div>

It is OK to complain! It can show bugs and that is very useful.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [August 20, 2021, 2:57pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/26 "2021-08-20T14:57:57Z")

</div>

Regarding Julia’s speed and accuracy for ODEs, Chris compares Julia’s solvers with solvers from other languages (including Matlab) here: [ODE Solver Multi-Language Wrapper Package Work-Precision Benchmarks (MATLAB, SciPy, Julia, deSolve (R))](https://benchmarks.sciml.ai/html/MultiLanguage/wrapper_packages.html)

…and a more-general comparison about the differential equation tools in several languages: [A Comparison Between Differential Equation Solver Suites In MATLAB, R, Julia, Python, C, Mathematica, Maple, and Fortran - Stochastic Lifestyle](https://www.stochasticlifestyle.com/comparison-differential-equation-solver-suites-matlab-r-julia-python-c-fortran/)

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [August 20, 2021, 3:00pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/27 "2021-08-20T15:00:21Z")

</div>

> [@Chaitanya](#):
>
> y=zeros(11,1)  
> for i=0:0.1:1  
> y(:,1)=sin(2\*i)  
> end

Welcome!

The _key_ error you make is that indexing arrays in Julia is done using _square brackets_, i.e., you should use `y[i,1] = sin(2*i)`. (I have also changed parenthesis arguments from `:,1` to `i,1`).

Next, in Julia, vector is a special data type, and there is no reason to make it into a column matrix. So it would be better to define `y` as `y = zeros(11)`, and insert values by `y[i] = sin(2*i)`.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 20, 2021, 3:05pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/28 "2021-08-20T15:05:20Z")

</div>

Julia’s DifferentialEquations suite has more efficient/modern high order rules than Matlab, as well as more specialized rules (such as adaptive SSP rules, high-order stochastic DEs, high-order delay DEs, etc.) so it should often be possible to get more accuracy with fewer function evaluations. See [A Comparison Between Differential Equation Solver Suites In MATLAB, R, Julia, Python, C, Mathematica, Maple, and Fortran - Stochastic Lifestyle](http://www.stochasticlifestyle.com/comparison-differential-equation-solver-suites-matlab-r-julia-python-c-fortran/)

Moreover, because your right-hand side function in Julia is compiled and fast (if it is written properly), even with the same number of function evaluations it is often vastly faster than Matlab (where performance is limited by the necessity of a user-code callback in the inner loop).

---

<div class="post-metadata">

### Author: ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)
#### Post date: [August 20, 2021, 5:22pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/29 "2021-08-20T17:22:58Z")

</div>

TY stillyslalom and stevengj.

---

<div class="post-metadata">

### Author: ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)
#### Post date: [August 20, 2021, 7:34pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/30 "2021-08-20T19:34:04Z")

</div>

So, finally this is how one would do it in Julia

```julia

for i in 1:1:11
    j = (i-1)/10
    y[i,1]=sin(2*j)
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: [August 20, 2021, 8:00pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/31 "2021-08-20T20:00:55Z")

</div>

> [@soldin](#):
>
> ```julia
> for i in 1:1:11
> j = (i-1)/10;
> y[i,1]=sin(2*j)
> end
> 
> ```

Like this:

```julia
for i in 1:11 #not 1:1:11
    j = (i-1)/10 # no semicolon
    y[i] = sin(2*j) # use a vector instead of a matrix
end

```

In Matlab, too, you should use `1:11`, not `1:1:11`.

---

<div class="post-metadata">

### Author: ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)
#### Post date: [August 20, 2021, 8:10pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/32 "2021-08-20T20:10:00Z")

</div>

Oops, that semi-colon was a hangover from MATLAB.

> Blockquote In Matlab, too, you should use `1:11` , not `1:1:11`

So, does the former give better speed?

---

<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: [August 20, 2021, 8:17pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/33 "2021-08-20T20:17:16Z")

</div>

No, that won’t give you better speed. It’s just slightly more readable, maybe.

---

<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: [August 20, 2021, 8:23pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/34 "2021-08-20T20:23:27Z")

</div>

Probably not. It gives some more information to the compiler, but I doubt it matters. But it is more idiomatic.

---

<div class="post-metadata">

### Author: ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)
#### Post date: [August 20, 2021, 8:24pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/35 "2021-08-20T20:24:21Z")

</div>

Ok, but the perfectionist in me prefers the full form.

---

<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: [August 20, 2021, 8:29pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/36 "2021-08-20T20:29:25Z")

</div>

It is more correct imho to write `1:11`. Why do you think this syntax exists? It’s actually the perfectionist in me that makes me point this out to you, when it’s really quite unimportant.

If you write 1:1:11, you are saying that a certain number of elements should be skipped. That number is zero, but it’s still a bit strange.

Would you write `1-1` instead of `0`?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 20, 2021, 8:42pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/37 "2021-08-20T20:42:43Z")

</div>

I would perhaps only say that no one uses `1:1:11`, and after getting a little bit more comfortable with the language that will not look quite natural. It feels like my insistence in indenting blocks with only two spaces… I am finally after a year of looking at others’ people code just finding four spaces more natural, and starting to follow the herd.

---

<div class="post-metadata">

### Author: ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)
#### Post date: [August 20, 2021, 8:45pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/38 "2021-08-20T20:45:39Z")

</div>

> Blockquote It’s actually the perfectionist in me that makes me point this out to you, when it’s really quite unimportant.

It’s perfectly okay! I, too, like nitpicking 🙂  
The reason why I prefer 1:1:10 over 1:10 is that the former is a general syntax (the increment may also be other than 1, like 0.5 or 2).

---

<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: [August 20, 2021, 9:03pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/39 "2021-08-20T21:03:46Z")

</div>

> [@soldin](#):
>
> The reason why I prefer 1:1:10 over 1:10 is that the former is a general syntax (the increment may also be other than 1, like 0.5 or 2).

Yes, that’s exactly why `1:11` is better. It says to take _all_ elements in an interval instead of ‘skip zero elements in each jump’. It’s _because_ it’s less general that it’s better, it structurally encodes that all elements must be included.

---

<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: [August 22, 2021, 1:07pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/40 "2021-08-22T13:07:11Z")

</div>

> [@DNF](#):
>
> BTW, `0:0.1:1` itself isn’t _just_ an iterator. It’s a real vector that can be used for everything an ordinary vector can be used for, with one exception: it’s _immutable_ , meaning you cannot change its values.

This is true for many intents and purposes, but not true in general, at least not on the latest release of julia

```julia
julia> 0 * (1:10)
ERROR: ArgumentError: step cannot be zero

```

For v1.7, this caveat has gone away

```julia
julia> 0 * (1:10)
StepRangeLen(0, 0, 10)

```

[Previous page](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317.md?page=1)

[Next page](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317.md?page=3)
