about single point crossover??

5 ビュー (過去 30 日間)
eman ahmed
eman ahmed 2016 年 11 月 20 日
編集済み: eman ahmed 2016 年 11 月 20 日
i'm writing aprog, convert word to numbers then binary then want to make crossover with random chromosome,the problem face me when the word is 3 letters output 4..and when 2 letters return error..can anyone help me plz
%convert the word 'THIS' to numbers according to the alphabetic sequence
prompt = 'Enter your text: ';
text= input(prompt,'s');
alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
[~,pos] = ismember(text,alphabet)
%convert each letter of the word to binary value
step3=de2bi(pos,'left-msb')
n = size(step3, 1);
l = size(step3, 2);
%generate the default chromosome (second parent)
Step4= randi([0, 1], 1,l)
Step4=[Step4;Step4;Step4;Step4];
%Choose a crossover point
cp = randperm(l-1, 1)
for i=1:n
breeders = [step3(i,:);Step4];
b1(i,:) = [breeders(1, 1:cp), breeders(2, cp+1:end)];
b2(i,:) = [breeders(2, 1:cp), breeders(1, cp+1:end)];
i=i+1;
end
b1=b1
b2=b2

回答 (0 件)

カテゴリ

Help Center および File ExchangeEncryption / Cryptography についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by