matlab交叉编译怎么编写
❶ 问一下大神matlab应该怎么编程
matlab 程序的文件代码是以.m文件的形式呈现的。将matlab代码编写进.m文件内然后运行即可。 例子: 建立一个 helloworld.m 文件内包括内容如下: fprintf('Hello World!'); 使用快捷键F5直接运行,然后可以在控制台下看到打印的: Hello World!
❷ matlab遗传算法中的交叉算子函数应该怎么编写
function [xv,fv]=myGA(fitness,a,b,NP,NG,Pc,Pm,eps)
L = ceil(log2((b-a)/eps+1)); %根据离散精度,确定二进制编码需要的码长
x = zeros(NP,L);
for i=1:NP
x(i,:) = Initial(L); %种群初始化
fx(i) = fitness(Dec(a,b,x(i,:),L)); %个体适应值
end
for k=1:NG
sumfx = sum(fx); %所有个体适应值之和
Px = fx/sumfx; %所有个体适应值的平均值
PPx = 0;
PPx(1) = Px(1);
for i=2:NP %用于轮盘赌策略的概率累加
PPx(i) = PPx(i-1) + Px(i);
end
for i=1:NP
sita = rand();
for n=1:NP
if sita <= PPx(n)
SelFather = n; %根据轮盘赌策略确定的父亲
break;
end
end
Selmother = floor(rand()*(NP-1))+1; %随机选择母亲
posCut = floor(rand()*(L-2)) + 1; %随机确定交叉点
r1 = rand();
if r1<=Pc %交叉
nx(i,1:posCut) = x(SelFather,1:posCut);
nx(i,(posCut+1):L) = x(Selmother,(posCut+1):L);
r2 = rand();
if r2 <= Pm %变异
posMut = round(rand()*(L-1) + 1);
nx(i,posMut) = ~nx(i,posMut);
end
else
nx(i,:) = x(SelFather,:);
end
end
x = nx;
for i=1:NP
fx(i) = fitness(Dec(a,b,x(i,:),L)); %子代适应值
end
end
fv = -inf;
for i=1:NP
fitx = fitness(Dec(a,b,x(i,:),L));
if fitx > fv
fv = fitx; %取个体中的最好值作为最终结果
xv = Dec(a,b,x(i,:),L);
end
end
function result = Initial(length) %初始化函数
for i=1:length
r = rand();
result(i) = round(r);
end
function y = Dec(a,b,x,L) %二进制编码转换为十进制编码
base = 2.^((L-1):-1:0);
y = dot(base,x);
y = a + y*(b-a)/(2^L-1);
❸ matlab如何编程
首先,在我们的桌面上找到matlab软件的图标,将鼠标移到该位置双击点开,这时电脑屏幕会弹出matlab软件的主界面。
这只是我的一点个人经验,还是希望能够帮助大家的。