# 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:** 1

<div class="post-metadata">

### Author: ![Chaitanya](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Chaitanya](https://discourse.julialang.org/u/Chaitanya)
#### Post date: [August 13, 2021, 4:15am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/1 "2021-08-13T04:15:00Z")

</div>

Hello Everyone. I have recently shifted to Julia from matlab. Storing data in julia with for loops has been a trouble for me  
Here, is the simple matlab code. I would appreciate if anyone could help me out the conversion in julia.

y=zeros(11,1)  
for i=0:0.1:1  
y(:,1)=sin(2\*i)  
end  
This code in matlab gives we a column vector of y from 0 to 1 in steps of 0.1.  
Please help me out how can I do this in julia .  
Thanks!

---

<div class="post-metadata">

### Author: ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)
#### Post date: [August 13, 2021, 4:20am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/2 "2021-08-13T04:20:26Z")

</div>

```julia
y = collect(0:0.1:1)

```

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [August 13, 2021, 4:35am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/3 "2021-08-13T04:35:20Z")

</div>

First of all welcome to the forum and great that you start with Julia 🙂 I did this transition a few years ago and I like Julia still (and Matlab even less).

A small side note: If you put your code into three backticks like

````julia
```
My Code
```

````

It looks like

```julia
My Code

```

Which is nicer to read.  
Then I think you might have meant to type `y(i,1) = I`? otherwise you always set the whole vector to I (and the end one is a vector of ones. The Matlab variant can also be done with `y =( 0:0.1:1)'`(the `(...)'` to get the column vector you have.

As Adriel wrote, in Julia `0:0.1:1` gives you an iterator – that is, if you only need it to run through something (another for for example) you avoid allocating the memory.  
The `collect(0:0.1:1)` says, that it should be collected into a vector.

I hope that explanation helps and have fun in learning Julia.

---

<div class="post-metadata">

### Author: ![Chaitanya](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Chaitanya](https://discourse.julialang.org/u/Chaitanya)
#### Post date: [August 13, 2021, 4:35am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/4 "2021-08-13T04:35:58Z")

</div>

Yeah, but what if y is some function of i.  
like Y(:,1)= sin(i) where I wanted to store all the values of sin wave in y.

---

<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 13, 2021, 4:38am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/5 "2021-08-13T04:38:33Z")

</div>

Then you can write

```julia
sin.(0:0.1:1)

```

And in Matlab,

```julia
sin(0:0.1:1)'

```

---

<div class="post-metadata">

### Author: ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)
#### Post date: [August 13, 2021, 4:40am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/6 "2021-08-13T04:40:08Z")

</div>

```julia
y = sin.(0:0.1:1)

```

Notice the dot before the `(`. Without the dot this code will try to compute the sine of the range object `0:0.1:1`, which obviously makes no sense.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [August 13, 2021, 4:42am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/7 "2021-08-13T04:42:31Z")

</div>

Uh then it gets nice. You can use several ways (and a for loop it the boring way)

DNF already mentioned the first, `.` is the elementwise-operator, so `sin.(0:0.1:1)` is apply `sin` to even element of the `0:0.1:1` (it gets `collect`ed on the way) – indeed the `.` operator (do something for even element of a vector/matrix) is one of my favourite features, it works with all functions (even your own) so that makes a lot of things nicer.

I also like [Comprehensions](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions) for this

```julia
y = [sin(i) for i in 0:0.1:1]

```

especially if the term is a little more involved.

---

<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 13, 2021, 4:46am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/8 "2021-08-13T04:46:46Z")

</div>

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.

In 99% of cases you don’t need to use `collect` on it to make it a ‘real vector’, just use `0:0.1:1` directly for whatever you want.

---

<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 13, 2021, 4:50am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/9 "2021-08-13T04:50:49Z")

</div>

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

The equivalent of this is either

```julia
sin.(2 .* (0:0.1:1)) # the space between 2 and .* is important 

```

or, if you _really_ need a loop

```julia
y = zeros(11)
for i in 0:0.1:1
    y[i] = sin(2*i)
end

```

Or an array comprehension, like @kellertuer showed.

---

<div class="post-metadata">

### Author: ![Chaitanya](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Chaitanya](https://discourse.julialang.org/u/Chaitanya)
#### Post date: [August 13, 2021, 4:54am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/10 "2021-08-13T04:54:59Z")

</div>

Thank you for the Information sir.

---

<div class="post-metadata">

### Author: ![Chaitanya](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Chaitanya](https://discourse.julialang.org/u/Chaitanya)
#### Post date: [August 13, 2021, 4:55am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/11 "2021-08-13T04:55:50Z")

</div>

Thank you sir. It helps me a lot.

---

<div class="post-metadata">

### Author: ![Chaitanya](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Chaitanya](https://discourse.julialang.org/u/Chaitanya)
#### Post date: [August 13, 2021, 5:01am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/12 "2021-08-13T05:01:44Z")

</div>

> [@DNF](#):
>
> ```julia
> y = zeros(11)
> for i in 0:0.1:1
> y[i] = sin(2*i)
> end
> 
> ```

Sir, the problem with this is, indexing cannot start with zero.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [August 13, 2021, 5:04am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/13 "2021-08-13T05:04:42Z")

</div>

Indexing also can only be integers – thats why I proposed comprehensions.

---

<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 13, 2021, 5:46am UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/14 "2021-08-13T05:46:51Z")

</div>

> [@Chaitanya](#):
>
> indexing cannot start with zero.

🤦‍♂️ This is what I get for answering on my phone. Also, you tricked me a bit with your Matlab example, which has a similar problem 😉

Here’s a better one:

```julia
y = zeros(11)
for (i, val) in pairs(0:0.1:1)
    y[i] = sin(2*val)
end

```

But really, you should use `y = sin.(2 .* (0:0.1:1))`.

Meanwhile, here’s what your Matlab code does:

```julia
y=zeros(11,1)
for i=0:0.1:1
y(:,1)=sin(2*i)
end

>> y
y =
    0.9093
    0.9093
    0.9093
    0.9093
    0.9093
    0.9093
    0.9093
    0.9093
    0.9093
    0.9093
    0.9093

```

It’s just the same value 10 times. Not what you wanted, I presume.

---

<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, 1:26pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/15 "2021-08-20T13:26:33Z")

</div>

```julia
y=zeros(11,1);
for i=1:1:11
j = (i-1)/10;
y(i,1)=sin(2*j);
end

```

This is how it should be written in MATLAB. Btw, I just decided to shift from Matlab (had written only a few codes, anyway) to Julia for comp. biol. work, though Matlab will probably still be my language of choice for differential equations.

---

<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, 1:44pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/16 "2021-08-20T13:44:25Z")

</div>

> [@soldin](#):
>
> though Matlab will probably still be my language of choice for differential equations.

that’s the main reason I would switch to julia

---

<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, 1:56pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/17 "2021-08-20T13:56:55Z")

</div>

I see. Would you like to enumerate some of the advantages that Julia has over MATLAB or post a link of an article on it?

---

<div class="post-metadata">

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

</div>

Speed. especially on non vectorized operations, such as for loops. The julia diffeq stack together with the SciML stack is probably the best there is, in both ease of use, versatility and speed on the area. There are many others, but other people can give them.

---

<div class="post-metadata">

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

</div>

differential equation is probably the most uncontroversial killer feature / ecosystem in Julia:  
[https://diffeq.sciml.ai/stable/](https://diffeq.sciml.ai/stable/)

> **[SciML: Open Source Software for Scientific Machine Learning](https://sciml.ai/news/)**
>
> Open Source Software for Scientific Machine Learning

> **[DiffEqFlux.jl – A Julia Library for Neural Differential Equations](https://julialang.org/blog/2019/01/fluxdiffeq/)**
>
> DiffEqFlux.jl – A Julia Library for Neural Differential Equations...

---

<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:16pm UTC](https://discourse.julialang.org/t/conversion-from-matlab-to-julia/66317/20 "2021-08-20T14:16:29Z")

</div>

Ok. TY gbaraldi and jling. Yes speed is certainly a factor. 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 well.

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