for loop each time create matrix ,i set if loop which check row total==9 ,if not proper than loop again create and check conditiion but here condition not follow and create matrix

1 次查看(过去 30 天)
clc
close all;
clear all;
X = 0:9 ; %three variables
Y=X ;
Z = X ;
objfun=[(X-0.33).^2+(Y-0.33).^2+(Z-0.33).^2] ; %objective function
for k=1:4 %set population generation limit here
for k1=1:1000 %loop generate MAX 1000 time digit to get population
pop=randi([0 7],1, 3 ); %random genertor limit n*3 mean n-row and 3-three column
tot=sum(pop,2); %total of each raw
if tot ==9 %check total of raw
entry(k,:)=pop; %if yes than take valid entry
break; %if yes than again valid raw in entry matrix
end %if no than again new raw generate
end
end
pop=entry %this entry matrix check all row total each time take valid entry only of row total=9
X= entry(:,1) ; Y = entry(:,2) ; Z= entry(:,3);
objs=[(X-0.33).^2+(Y-0.33).^2+(Z-0.33).^2]
swappedcolumns = [7 8]; %swapping digit in string
finalvalue=zeros(1,3); %generate zero matrix
totalofraw=zeros(1,3); %generate zero matrix
%problem start from here
for u=1:6 %same limit set with k loop max value
bins=fliplr(de2bi(pop,8));
child = bins;
swapoddeven = reshape([2:2:size(bins, 1);
1:2:size(bins, 1)], 1, []);
child(swapoddeven, swappedcolumns) = child(1:size(bins, 1),swappedcolumns);
bincon=bi2de(fliplr(child(:,:)));
cross1=reshape(bincon,4,3); %change here value according i value in upper for loop
p=reshape(bincon.',3,[]);
pop=p';
total=sum(pop,2);
if total==9
newentry(u,:)=pop;
break;
end
newentry=pop %iwant this pop matrix each raw total=9 is valid if not total=9 than again loop rotate
X= pop(:,1) ; Y = pop(:,2) ; Z= pop(:,3);
objs=[(X-0.33).^2+(Y-0.33).^2+(Z-0.33).^2]
finalvalue(u)=objs(u);
fog(u,:)=[objs];
end
each newentry matrix row total=9 if it yes than valid if not total=9 than again loop rotate

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by