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軟體的主界面。
這只是我的一點個人經驗,還是希望能夠幫助大家的。