# Mixed Type Variables in Julia

**URL:** <https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989>\
**Category:** General Usage\
**Tags:** package\
**Created:** [March 18, 2019, 7:00am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989 "2019-03-18T07:00:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [March 18, 2019, 7:00am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/1 "2019-03-18T07:00:57Z")

</div>

Dear All,

How can we fix the issues from mixed type variable in Julia? now I convert these loops from Matlab to Julia; However, It seems not working in Julia …

**Matlab**

> p = 1.0;  
> pr = 3.0;  
> tp = 2.0;  
> for i = 1:fix(pr/2)  
> inv = 1/nchoosek(pr,i);  
> mpi = min(p,i);  
> for j = max(0,i-tp):mpi  
> bezalfs(i+1,j+1) = inv\*nchoosek(p,j)\*nchoosek(tp,i-j);  
> bezalfs(pr-i+1,p-j+1) = bezalfs(i+1,j+1);  
> end  
> end

**Julia:**

> p = 1.0  
> pr = 3.0  
> tp = 2.0  
> bezalfs = Array{Float64}(undef, 4, 2)
> 
> for i = 1:round(pr/2)  
> inv = 1/binomial(pr,i)  
> mpi = min(p,i)  
> for j = max(0,i-tp):mpi  
> bezalfs[i+1,j+1] = inv\*binomial(p,j)\*binomial(tp,i-j);  
> bezalfs[pr-i+1,p-j+1] = bezalfs[i+1,j+1];  
> end  
> end

Solution(Matlab)

> bezalfs =
> 
> ```
> 0 0
> 0.6667 0.3333
> 0.3333 0.6667
> 
> ```

**Question: how can we solve this bug in a brilliant way in Julia?**

---

<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 18, 2019, 7:12am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/2 "2019-03-18T07:12:53Z")

</div>

It would help if you could include the error message, or what’s wrong with the output. However you may be looking for `÷`, as in [Mathematics · The Julia Language](https://docs.julialang.org/en/v1/base/math/#Base.div)

---

<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 18, 2019, 7:24am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/3 "2019-03-18T07:24:50Z")

</div>

@improbable22 is right. Use `div(pr, 2)` or `pr ÷ 2` or `pr >> 1` instead of `round(pr/2)`, to get an integer.

Also, your preallocated matrix is 4-by-2 instead of 3-by-2 (you should probably calculate that size instead of hardcoding it, though).

Furthermore, please wrap your code in triple backticks (`````), to make it easier to read, like this:

```julia
p = 1
pr = 3
tp = 2
bezalfs = Array{Float64}(undef, 3, 2) # <- 3 not 4
for i in 1:div(pr, 2) # div not /
    inv = 1/binomial(pr,i)
    mpi = min(p,i)
    for j in max(0,i-tp):mpi
        bezalfs[i+1,j+1] = inv*binomial(p,j)*binomial(tp,i-j);
        bezalfs[pr-i+1,p-j+1] = bezalfs[i+1,j+1];
    end
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:** [March 18, 2019, 7:33am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/4 "2019-03-18T07:33:32Z")

</div>

BTW, I see that I pointed you to this thread ([Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)) less than a week ago, and specifically mentioned the use of triple backticks.

It would perhaps be a good idea to re-read that post.

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [March 18, 2019, 10:37am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/5 "2019-03-18T10:37:59Z")

</div>

> [@DNF](#):
>
> p = 1 pr = 3 tp = 2 bezalfs = Array{Float64}(undef, 3, 2) # \<- 3 not 4 for i in 1:div(pr, 2) # div not / inv = 1/binomial(pr,i) mpi = min(p,i) for j in max(0,i-tp):mpi bezalfs[i+1,j+1] = inv\*binomial(p,j)\*binomial(tp,i-j); bezalfs[pr-i+1,p-j+1] = bezalfs[i+1,j+1]; end end

Thanks for your answer!

```julia
p = 1.0
pr = 3.0
tp = 2.0
bezalfs = Array{Float64}(undef, 3, 2) # <- 3 not 4
for i in 1:div(pr, 2) # div not /
    inv = 1/binomial(pr,i)
    mpi = min(p,i)
    for j in max(0,i-tp):mpi
        bezalfs[i+1,j+1] = inv*binomial(p,j)*binomial(tp,i-j);
        bezalfs[pr-i+1,p-j+1] = bezalfs[i+1,j+1];
    end
end

```

if input variable is double type, how can we deal with it **brilliantly** in Julia?

---

<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 18, 2019, 10:52am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/6 "2019-03-18T10:52:21Z")

</div>

What’s the input variable? Does “double” mean `Float64`?

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [March 18, 2019, 10:56am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/7 "2019-03-18T10:56:49Z")

</div>

yes,

In Matlab, we can use

```julia
 p = 1.0
pr = 3.0
tp = 2.0

```

But in Julia,

```julia

p = 1.0
pr = 3.0
tp = 2.0

```

will never work, untill we switch these to integer variable …

---

<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 18, 2019, 11:29am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/8 "2019-03-18T11:29:42Z")

</div>

Assuming I understand the question correctly, you can use `floor(Int, pr/i)` or `Int(div(pr, i))`. That should work both for integers and floats.

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [March 18, 2019, 11:33am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/9 "2019-03-18T11:33:52Z")

</div>

yes, you are fully correkt! but why do we need [JuliaStats/Distributions.jl](https://github.com/JuliaStats/Distributions.jl) package to solve this problem? such solution seems too too complex.

---

<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 18, 2019, 11:45am UTC](https://discourse.julialang.org/t/mixed-type-variables-in-julia/21989/10 "2019-03-18T11:45:31Z")

</div>

You don’t need it. Why do you think that you do?
