0

我在 MATLAB 中有一个程序。我想运行程序 10,000 次。对于每次运行,我都会得到一个最终输出作为计数。我想要的是将每次迭代的计数值导出到全局数组或 Excel 电子表格。我想最后检查应该包含 10,000 个值的计数数组。

请各位大神给点建议。我也在贴代码。

%%steps
%1.Initialize the populations 
tic
clc; clear all; close all;
par_size=10;
%Encode
for i=1:par_size
    par(i,:)=dec2bin(round(1+31*rand),10);
    a(i) = bin2dec(par(i,:));
end
orig_par=par;
fitness = zeros(1,par_size);
count=0;
while sum(fitness)~=1000
    count=count+1;
%%
%2.Evaluate fitness
fitness = zeros(1,par_size);
%The fitness function
%count the no of one's in the string
fitness = [(sum(par=='1',2)).*(sum(par=='1',2))].';
sum_fitness = sum(fitness)
prob_fitness = fitness./sum_fitness;
[~,I]=sort(prob_fitness);
%I is the order of the fitness value
prob_fitness_sort = prob_fitness(I);
%sorting
par = par(I,:);

%%
%3.Select Parents
%Roulette wheel selection
%generate cumulative probability
cum_prob = zeros(size(prob_fitness));
A = zeros(size(prob_fitness));
for i=1:par_size
    A(i) = prob_fitness_sort(i);
    cum_prob(i) = sum(A);
end
new_pop = par;
%Selection being done
r1 = zeros(1,par_size);
for i=1:par_size
    r1(i) = rand;
    if r1(i)<=cum_prob(1)
        new_pop(i,:) = par(1,:);
    elseif r1(i)>cum_prob(1) && r1(i)<=cum_prob(2)
        new_pop(i,:) = par(2,:);
    elseif r1(i)>cum_prob(2) && r1(i)<=cum_prob(3)
        new_pop(i,:) = par(3,:);
    elseif r1(i)>cum_prob(3) && r1(i)<=cum_prob(4)
        new_pop(i,:) = par(4,:);
    elseif r1(i)>cum_prob(4) && r1(i)<=cum_prob(5)
        new_pop(i,:) = par(5,:);
    elseif r1(i)>cum_prob(5) && r1(i)<=cum_prob(6)
        new_pop(i,:) = par(6,:);
    elseif r1(i)>cum_prob(6) && r1(i)<=cum_prob(7)
        new_pop(i,:) = par(7,:);
    elseif r1(i)>cum_prob(7) && r1(i)<=cum_prob(8)
        new_pop(i,:) = par(8,:);
   elseif r1(i)>cum_prob(8) && r1(i)<=cum_prob(9)
        new_pop(i,:) = par(9,:);
   elseif r1(i)>cum_prob(9) && r1(i)<=cum_prob(10)
        new_pop(i,:) = par(10,:);
   end
end
%new_pop is the new particle sample

%%
%4.Crossover and Mutate

%crossover
pc = 0.6;   %pc is the crossover probability
for i=1:2:par_size %selecting adjacent couples
     r2 = rand;
    if r2<=pc
        l = size(new_pop, 2);
        %choose the parents
        %here adjacent parents are taken
        breeders(1,:)=new_pop(i,:);
        breeders(2,:)=new_pop(i+1,:);
        %choose a crossover point
        cp = randperm(l, 1);            
        %do crossover
        b1 = [breeders(1, 1:cp), breeders(2, cp+1:end)];
        b2 = [breeders(2, 1:cp), breeders(1, cp+1:end)];
        new_pop(i,:) = b1;
        new_pop(i+1,:) = b2;
    end
end


%mutation
%pm be mutation probability
pm = 0.01;
for i=1:par_size
    r2 = rand;
    if r2<=pm %mutation probability is 0.1
        %Do mutation
        %select random position
        r3 = 1+round((10-1)*rand);           
        new_pop(i,r3)=num2str(abs(str2num(new_pop(i,r3))-1));
    end
end
par = new_pop;
end
orig_par
par
count
toc

不用担心程序会成功运行。如您所见,每次运行程序结束时都会显示作为计数生成的值。如果可能,我想将此值导出到全局数组或 MATLAB 中的 excel 电子表格。

提前致谢。

如有任何疑问,请发表评论!

4

1 回答 1

0

您可以创建一个变量count_nb并将其初始化为zeros(10000,1),并在 while 循环内(之后count = count + 1)具有count_nb(count) = count. 一旦在while循环之外,您可以count_nb使用将变量写入 Excelxlswrite或将其保存到 MAT 文件,或将其写入文本文件等...

于 2013-07-18T11:48:00.727 回答