First steps to programming

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

What have you tried?

1 Like
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);
1 Like

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 Vectors

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

5 Likes

Thank you so much for your answears and helps

slight_smile:

i did this code by Matlab

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?

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
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

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

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.

1 Like

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

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

to begin you can try this also: MATLAB to Julia translator | MATLAB to Julia converter

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

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

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

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