MATLAB: How to count and terminate inside iteration routine when the matrix (i,j) reaches a popsize(j)

for loopifMATLAB

I have the following routine working:
%New Function
repeat=50;
i=0;
j=0;
r=0;
k=50;
l=0;
npop=10;
maxiteration=100000;
popmin=100;
popmax=10000;
popcheck=0;
pop1=0;
pop2=0;
popsize(npop)=0;
town(npop,popmax)=0;
En(npop,repeat)=0;
townsum(npop)=0;
for i=1:npop
popsize(i)=round(100+rand()*10000);
if popsize>popmax
popsize=popmax;
end
townsum(i)=0;
for r=1:repeat
for j=1:maxiteration
randompick1=1+round(rand()*popsize(i));
randompick2=1+round(rand()*popsize(i));
town(i,randompick1)=1;
town(i,randompick2)=1;
En(i,r)=j;
end
end
end
What I need is to count the iterations it takes to fill each row of the matrix town(i,j) to town(i,popsize(i)). I have tried if, while and keep running afoul of left operator being invalid. En(i,j) is number of iteration it actually takes to fill town(i,j) from town(i,1) to town(i,popsize(i)) with 1's. I need to test sum of town(i,j) until town(i,j)=popsize(i). Any thoughts or help is appreciated.

Best Answer

This will fill up the row and bail out once all elements have been set to 1.
row=0;
j=0;
r=0;
k=50;
l=0;
npop=10;
maxiteration=100000;
popmin=100;
popmax=10000;
popcheck=0;
pop1=0;
pop2=0;
popsize(npop)=0;
town(npop, popmax)=0;
En(npop)=0;
townsum(npop)=0;
rowIsFull = true(popmax, 1);
for row=1:npop
popsize(row)=round(100+rand()*10000);
if popsize>popmax
popsize=popmax;
end
townsum(row)=0;
j = 1;
while j <= maxiteration
randompick1=1+round(rand()*popsize(row));
randompick2=1+round(rand()*popsize(row));
town(row,randompick1)=1;
town(row,randompick2)=1;
En(row)=j;
j = j + 1;
if sum(town(row,:)) == popmax
% All columns are 1 now.
break; % Out of while
end
end
end
Of course that will never happen, or at least never happen for a very very long time. Do you know why?
Related Question