# First steps to programming

**URL:** <https://discourse.julialang.org/t/first-steps-to-programming/9436>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 2, 2018, 12:09am UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436 "2018-03-02T00:09:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 2, 2018, 12:09am UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/1 "2018-03-02T00:09:05Z")

</div>

```matlab
function [y1,y2]=Croisement(ch1,ch2)
%ch1 chromosome 1;
%ch2 chromosome 2;
nVar=numel(ch1);
c=randi([1 nVar-1]);
y1=[ch1(1:c) ch2(c+1:end)]
y2=[ch2(1:c) ch1(c+1:end)]
end

```

please how can i write this code by julia ( now this code is done by matlab)  
please I need your help

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 2, 2018, 1:41am UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/2 "2018-03-02T01:41:51Z")

</div>

What have you tried?

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [March 2, 2018, 3:44am UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/3 "2018-03-02T03:44:06Z")

</div>

```julia
function Croisement(ch1, ch2)
  ##ch1 chromosome 1
  ##ch2 chromosome 2
  n = length(ch1);
  c = ceil(Int32, rand()*(n-1)); ## selects from 1 to n-1
  y1 = [ch1[1:c]; ch2[c+1:end]];
  y2 = [ch2[1:c]; ch1[c+1:end]];
  return(y1, y2);
  end;

y1, y2 = Croisement(ch1, ch2);
```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 2, 2018, 3:58am UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/4 "2018-03-02T03:58:24Z")

</div>

That works fine. Some suggestions:

- you don’t need the `;` at the end of each line in Julia
- `c = rand(1 : n - 1)` is perhaps easier and more efficient than `c = ceil(Int32, rand()*(n-1))`
- you could think about creating a `Chromosome` type instead of using raw `Vector`s

There are additional tips I could give you if this is performance-critical, so let me know if that’s the case.

---

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 18, 2018, 12:20pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/5 "2018-03-18T12:20:12Z")

</div>

Thank you so much for your answears and helps

slight\_smile:

---

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 18, 2018, 12:21pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/6 "2018-03-18T12:21:04Z")

</div>

i did this code by Matlab

```julia
function[c]=croisement2(c1,c2)
%fonction de croisement en deux points
% c1 et c2 sonts les deux parents fils de premiere croisements
n=size(c1,2)-2;
k1=c1;k2=c2;
l=randi(n-1,1,2);
l1=min(l);
l2=max(l);
c1(l1:l2)=k2(l1:l2);
c2(l1:l2)=k1(l1:l2);
c=[c1 c2];
end

```

and I tried to convert it in julia please I need your help is it true?

```julia
function croisement2(c1,c2)
%fonction de croisement en deux points
% c1 et c2 sonts les deux parents fils de premiere croisements
n=size(c1,2)-2;
k1=c1 ; k2=c2;
l=randi(n-1,1,2);% I don't know how to convert this commond in julia
l1=min(l)
l2=max(l)
c1(l1:l2)=k2(l1:l2)
c2(l1:l2)=k1(l1:l2)
c=[c1 c2]
return (c)
end
c= Croisement2(c1,c2)
Thank you so much

```

---

<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:** [March 18, 2018, 12:24pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/7 "2018-03-18T12:24:04Z")

</div>

```julia
l=randi(n-1,1,2);% I don't know how to convert this commond in julia

```

See e.g. [Generating a random integer in range in Julia - Stack Overflow](https://stackoverflow.com/questions/24326876/generating-random-integer-in-range-in-julia)

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [March 18, 2018, 2:32pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/8 "2018-03-18T14:32:57Z")

</div>

The following Julia code should be the equivalent of your MATLAB function:

```julia
function croisement2(c1,c2)
	n = size(c1,2)-2
	k1 = copy(c1)
	k2 = copy(c2)
	l = rand(1:n-1,1,2)
	l1, l2 = extrema(l)
	c1[l1:l2] = k2[l1:l2]
	c2[l1:l2] = k1[l1:l2]
	return [c1 c2]
end

```

I encourage you, though, to read Julia’s [Noteworthy Differences from other Languages](https://docs.julialang.org/en/release-0.4/manual/noteworthy-differences/).

---

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 18, 2018, 2:36pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/9 "2018-03-18T14:36:09Z")

</div>

Thank you so much for your help really thanks to your help I advance slowly

---

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 18, 2018, 2:37pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/10 "2018-03-18T14:37:02Z")

</div>

Thank you so much for your help really thanks to your help I advance slowly, I will read it with pleasure

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [March 18, 2018, 2:43pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/11 "2018-03-18T14:43:08Z")

</div>

to begin you can try this also: [MATLAB to Julia translator | MATLAB to Julia converter](http://sciencecow.mit.edu/matlab-to-julia/)

---

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 18, 2018, 3:07pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/12 "2018-03-18T15:07:52Z")

</div>

Thank you so much it’s an honour for be to receive your help I will read it …  
that encourages me so much …

---

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 18, 2018, 3:08pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/13 "2018-03-18T15:08:42Z")

</div>

Thank you so much it’s an honour for be to receive your help I will read it …  
that encourages me so much …

---

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 19, 2018, 9:46pm UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/14 "2018-03-19T21:46:54Z")

</div>

please I tried to run this algorithme but I found problem ☹ I don’t know why

---

<div class="post-metadata">

**Author:** ![Imane\_Agnaou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imane_agnaou/32/3744_2.png) [@Imane\_Agnaou](https://discourse.julialang.org/u/Imane_Agnaou)\
**Post date:** [March 20, 2018, 12:24am UTC](https://discourse.julialang.org/t/first-steps-to-programming/9436/15 "2018-03-20T00:24:24Z")

</div>

Finally I could resolve it 🙂 i must just change some indexes …Thank you
