n_outerrun = 1000;
n_runs=10000;
table4 = table();
count_allruns_store = zeros(n_outerrun, 1);
for outerrun = 1:n_outerrun
rng('shuffle');
i = zeros(n_runs,1);
d = zeros(n_runs,1);
classres = zeros(n_runs,1);
count1 = zeros(n_runs,1);
count2 = zeros(n_runs,1);
sum1ch = zeros(n_runs, 1);
sum2ch = zeros(n_runs, 1);
class_dop = ones(n_runs, 1);
sum1_old2 = 0;
second_table = table();
a = 0.005;
b = rand();
c = rand();
e = rand();
myArray = [0.01, 0.1, 1, 10, 100];
myArray1 = [0, 0.01, 0.1, 0.5, 1, 2, 10, 100];
randomIndex = randi(numel(myArray));
randomIndex1 = randi(numel(myArray1));
o = myArray(randomIndex);
odop = myArray1(randomIndex1);
f = -1 + rand();
g = 10*rand();
k = min(b, c);
l = max(b, c)-min(b, c);
m = 1-max(b, c);
n = sqrt(e - g*log(rand()));
h = randn()*sqrt(min(b, c));
mui = f*h*sqrt(l/k);
sigmai = sqrt((1-f^2)*l);
p_i = makedist('Normal', mui, sigmai);
j = randn()*sqrt(m);
igrid3 = icdf(p_i, linspace(0,1,10001));
xedges3 = igrid3;
xedges3(1)=2*xedges3(2)-xedges3(3);
xedges3(10001)=2*xedges3(10000)-xedges3(9999);
disp(outerrun);
n_outerruninit = 50;
n_runsinit=400;
count_allruns_storeinit = zeros(n_outerruninit, 1);
second_tableinit = table();
sumudiffinit_values = zeros(n_outerruninit, 1);
for outerruninit = 1:n_outerruninit
rng('shuffle');
iinit = zeros(n_runsinit,1);
dinit = zeros(n_runsinit,1);
classresinit = zeros(n_runsinit,1);
count1init = zeros(n_runsinit,1);
count2init = zeros(n_runsinit,1);
sum1chinit = zeros(n_runsinit, 1);
sum2chinit = zeros(n_runsinit, 1);
class_dopinit = ones(n_runsinit, 1);
sum1_old2init = 0;
for runinit = 1:n_runsinit
classresinit(runinit) = randi([1,2]);
end
igrid2init = icdf(p_i, linspace(0,1,sqrt(n_runsinit)*50));
xedges2init = igrid2init;
for rungridinit1 = 1:sqrt(n_runsinit)
for rungridinit2 = 1:sqrt(n_runsinit)
mid_iinit = xedges2init(rungridinit1*50-25);
mid_dinit = (2/sqrt(n_runsinit))*rungridinit2-(1/sqrt(n_runsinit));
igrid2initdop = rungridinit2*sqrt(n_runsinit) - sqrt(n_runsinit) + rungridinit1;
iinit(igrid2initdop)=mid_iinit;
dinit(igrid2initdop)=mid_dinit;
end
end
count_onesinit = 0;
count_allrunsinit = 0;
meplus = mean(iinit(classresinit == 2));
meminus = mean(iinit(classresinit == 1));
mbplus = mean(dinit(classresinit == 2));
mbminus = mean(dinit(classresinit == 1));
veminus = std(iinit(classresinit == 1))*std(iinit(classresinit == 1));
pr = mean(classresinit)-1;
mbplustwo = zeros(n_runsinit, 1);
selectedIndices = cell(n_runsinit, 1);
for idop = 1:n_runsinit
valuei = iinit(idop);
[~, indices] = mink(abs(iinit - valuei), 3*sqrt(n_runsinit));
selectedIndices{idop} = indices;
selectedclass = classresinit(indices);
selectedd = dinit(indices);
mbplustwodop = mean(selectedd(selectedclass == 2));
if isnan(mbplustwodop)
mbplustwodop = 0;
end
mbplustwo(idop) = mbplustwodop;
end
rp1 = a*(n^2)*(pr*mbplus*m+(1-pr)*(veminus+mbminus*m))+a*(n^2)*pr*(1-pr)*((meplus-meminus).^2)...
+0.5*(a^3)*(n^4)*pr*(1-pr)*((mbplus*m-veminus-mbminus*m).^2)...
-1.5*(a^2)*(n^3)*pr*m*mean(dinit(classresinit == 2))*mean(iinit(classresinit == 2))...
-1.5*(a^2)*(n^3)*(1-pr)*(meminus*veminus+m*mean(dinit(classresinit == 1))*mean(iinit(classresinit == 1)))...
+1.5*(a^2)*(n^3)*(pr*meplus+(1-pr)*meminus)*(pr*mbplus*m+(1-pr)*(veminus+mbminus*m));
udiffinit = o*((dinit.*(n^2)*m-(n^2)*mbplustwo.*m).^2)...
+odop*((iinit.*n-a*(n^2)*mbplustwo.*m-n*pr*meplus-n*(1-pr)*meminus+rp1).^2)...
+((a*(n^2)*mbplustwo.*m).^2)-o*((dinit.*(n^2)*m-(n^2)*veminus-(n^2)*mbminus*m).^2)...
-odop*((a*(n^2)*(veminus+mbminus*m)+n*pr*meplus-n*pr*meminus-rp1).^2)...
-((iinit.*n-n*meminus+a*(n^2)*(veminus+mbminus*m)).^2);
classcorrectinit = zeros(n_runsinit,1);
for runinit = 1:n_runsinit
classcorrectinit(runinit) = (1.5-classresinit(runinit))*udiffinit(runinit);
if classcorrectinit(runinit) >=0
count1init(runinit) = 0;
count2init(runinit) = 0;
else
count1init(runinit) = abs(udiffinit(runinit));
count2init(runinit) = 1;
end
end
class_startinit = classresinit;
count1_startinit = count1init;
count2_startinit = count2init;
sum1_oldinit = sum(count1init);
sum2_oldinit = sum(count2init);
while true
max_count1init = max(count1init);
idx3init = find(count1init == max_count1init);
classresinit(idx3init) = 3 - classresinit(idx3init);
meplus = mean(iinit(classresinit == 2));
meminus = mean(iinit(classresinit == 1));
mbplus = mean(dinit(classresinit == 2));
mbminus = mean(dinit(classresinit == 1));
veminus = std(iinit(classresinit == 1))*std(iinit(classresinit == 1));
if isnan(veminus)
veminus = 0;
end
pr = mean(classresinit)-1;
proizvplus = mean(dinit(classresinit == 2))*mean(iinit(classresinit == 2));
proizvminus = mean(dinit(classresinit == 1))*mean(iinit(classresinit == 1));
if isnan(meplus)
meplus = 0;
mbplus = 0;
proizvplus = 0;
end
if isnan(meminus)
meminus = 0;
mbminus = 0;
proizvminus = 0;
end
mbplustwo = zeros(n_runsinit, 1);
for idop = 1:n_runsinit
valuei = iinit(idop);
indices = selectedIndices{idop};
selectedclass = classresinit(indices);
selectedd = dinit(indices);
mbplustwodop = mean(selectedd(selectedclass == 2));
if isnan(mbplustwodop)
mbplustwodop = 0;
end
mbplustwo(idop) = mbplustwodop;
end
rp1 = a*(n^2)*(pr*mbplus*m+(1-pr)*(veminus+mbminus*m))+a*(n^2)*pr*(1-pr)*((meplus-meminus).^2)...
+0.5*(a^3)*(n^4)*pr*(1-pr)*((mbplus*m-veminus-mbminus*m).^2)...
-1.5*(a^2)*(n^3)*pr*m*proizvplus-1.5*(a^2)*(n^3)*(1-pr)*(meminus*veminus+m*proizvminus)...
+1.5*(a^2)*(n^3)*(pr*meplus+(1-pr)*meminus)*(pr*mbplus*m+(1-pr)*(veminus+mbminus*m));
udiffinit = o*((dinit.*(n^2)*m-(n^2)*mbplustwo.*m).^2)...
+odop*((iinit.*n-a*(n^2)*mbplustwo.*m-n*pr*meplus-n*(1-pr)*meminus+rp1).^2)...
+((a*(n^2)*mbplustwo.*m).^2)-o*((dinit.*(n^2)*m-(n^2)*veminus-(n^2)*mbminus*m).^2)...
-odop*((a*(n^2)*(veminus+mbminus*m)+n*pr*meplus-n*pr*meminus-rp1).^2)...
-((iinit.*n-n*meminus+a*(n^2)*(veminus+mbminus*m)).^2);
classcorrectinit = zeros(n_runsinit,1);
for runinit = 1:n_runsinit
classcorrectinit(runinit) = (1.5-classresinit(runinit))*udiffinit(runinit);
if classcorrectinit(runinit) >=0
count1init(runinit) = 0;
count2init(runinit) = 0;
else
count1init(runinit) = abs(udiffinit(runinit));
count2init(runinit) = 1;
end
end
if (sum(count2init)==1)
count_onesinit = count_onesinit+1;
end
count_allrunsinit = count_allrunsinit+1;
count_allruns_storeinit(outerruninit) = count_allrunsinit;
min_mistakeinit = min(sum(count2init), sum2_oldinit);
if (sum(count2init) < 1) || ...
(count_onesinit == 10) || ...
(count_allrunsinit > 2999 && sum(count2init) > sum2_oldinit && ...
min_mistakeinit < sqrt(n_runsinit)) || ...
(count_allrunsinit == 4000)
break;
end
sum1_old2init = sum1_oldinit;
sum1_oldinit = sum(count1init);
sum2_oldinit = sum(count2init);
class_startinit = classresinit;
count1_startinit = count1init;
count2_startinit = count2init;
end
absudiffinit = abs(udiffinit);
sumudiffinit = sum(absudiffinit);
sumudiffinit_values(outerruninit) = sumudiffinit;
second_tableinit = [second_tableinit; table(iinit,dinit,classresinit, udiffinit, count1init, ...
count2init, sum1chinit, sum2chinit, class_startinit, count1_startinit, count2_startinit)];
end
reshapedMatrix = reshape(second_tableinit.classresinit, n_runsinit, n_outerruninit);
numSmallest = 0.5*n_outerruninit;
[sortedValues, sortedIndices] = sort(sumudiffinit_values, 'ascend');
indicesOfSmallest = sortedIndices(1:numSmallest);
reshapedMatrix1 = reshapedMatrix;
reshapedMatrix1(:, indicesOfSmallest) = [];
summedclassresinit = sum(reshapedMatrix1, 2);
threshold = 0.75*n_outerruninit;
avclassresinit = ones(size(summedclassresinit));
avclassresinit(summedclassresinit >= threshold) = 2;
for run = 1:n_runs
i(run) = random(p_i);
d(run) = 2*rand();
end
binEdges = icdf(p_i, linspace(0,1,sqrt(n_runsinit)+1));
binEdges(1) = min(i)-10;
binEdges(sqrt(n_runsinit)+1) = max(i)+10;
binEdgesvert = linspace(0,2,sqrt(n_runsinit)+1);
horindices = zeros(n_runs,1);
vertindices = zeros(n_runs,1);
for run = 1:n_runs
horindices(run) = find(i(run) <= binEdges, 1)-1;
vertindices(run) = find(d(run) <= binEdgesvert, 1)-1;
final_index = vertindices(run)*sqrt(n_runsinit)-sqrt(n_runsinit)+horindices(run);
classres(run) = avclassresinit(final_index);
end
count_ones = 0;
count_allruns = 0;
meplus = mean(i(classres == 2));
meminus = mean(i(classres == 1));
mbplus = mean(d(classres == 2));
mbminus = mean(d(classres == 1));
veminus = std(i(classres == 1))*std(i(classres == 1));
pr = mean(classres)-1;
mbplustwo = zeros(n_runs, 1);
selectedIndices = cell(n_runs, 1);
for idop = 1:n_runs
valuei = i(idop);
[~, indices] = mink(abs(i - valuei), 200);
selectedIndices{idop} = indices;
selectedclass = classres(indices);
selectedd = d(indices);
mbplustwodop = mean(selectedd(selectedclass == 2));
if isnan(mbplustwodop)
mbplustwodop = 0;
end
mbplustwo(idop) = mbplustwodop;
end
rp1 = a*(n^2)*(pr*mbplus*m+(1-pr)*(veminus+mbminus*m))+a*(n^2)*pr*(1-pr)*((meplus-meminus).^2)...
+0.5*(a^3)*(n^4)*pr*(1-pr)*((mbplus*m-veminus-mbminus*m).^2)...
-1.5*(a^2)*(n^3)*pr*m*mean(d(classres == 2))*mean(i(classres == 2))...
-1.5*(a^2)*(n^3)*(1-pr)*(meminus*veminus+m*mean(d(classres == 1))*mean(i(classres == 1)))...
+1.5*(a^2)*(n^3)*(pr*meplus+(1-pr)*meminus)*(pr*mbplus*m+(1-pr)*(veminus+mbminus*m));
udiff = o*((d.*(n^2)*m-(n^2)*mbplustwo.*m).^2)...
+odop*((i.*n-a*(n^2)*mbplustwo.*m-n*pr*meplus-n*(1-pr)*meminus+rp1).^2)...
+((a*(n^2)*mbplustwo.*m).^2)-o*((d.*(n^2)*m-(n^2)*veminus-(n^2)*mbminus*m).^2)...
-odop*((a*(n^2)*(veminus+mbminus*m)+n*pr*meplus-n*pr*meminus-rp1).^2)...
-((i.*n-n*meminus+a*(n^2)*(veminus+mbminus*m)).^2);
classcorrect = zeros(n_runs,1);
for run = 1:n_runs
classcorrect(run) = (1.5-classres(run))*udiff(run);
if classcorrect(run) >=0
count1(run) = 0;
count2(run) = 0;
else
count1(run) = abs(udiff(run));
count2(run) = 1;
end
end
class_start = classres;
count1_start = count1;
count2_start = count2;
sum1_old = sum(count1);
sum2_old = sum(count2);
while true
max_count1 = max(count1);
idx3 = find(count1 == max_count1);
classres(idx3) = 3 - classres(idx3);
meplus = mean(i(classres == 2));
meminus = mean(i(classres == 1));
mbplus = mean(d(classres == 2));
mbminus = mean(d(classres == 1));
veminus = std(i(classres == 1))*std(i(classres == 1));
if isnan(veminus)
veminus = 0;
end
pr = mean(classres)-1;
proizvplus = mean(d(classres == 2))*mean(i(classres == 2));
if isnan(meplus)
meplus = 0;
mbplus = 0;
proizvplus = 0;
end
proizvminus = mean(d(classres == 1))*mean(i(classres == 1));
if isnan(meminus)
meminus = 0;
mbminus = 0;
proizvminus = 0;
end
mbplustwo = zeros(n_runs, 1);
for idop = 1:n_runs
valuei = i(idop);
indices = selectedIndices{idop};
selectedclass = classres(indices);
selectedd = d(indices);
mbplustwodop = mean(selectedd(selectedclass == 2));
if isnan(mbplustwodop)
mbplustwodop = 0;
end
mbplustwo(idop) = mbplustwodop;
end
rp1 = a*(n^2)*(pr*mbplus*m+(1-pr)*(veminus+mbminus*m))+a*(n^2)*pr*(1-pr)*((meplus-meminus).^2)...
+0.5*(a^3)*(n^4)*pr*(1-pr)*((mbplus*m-veminus-mbminus*m).^2)...
-1.5*(a^2)*(n^3)*pr*m*proizvplus-1.5*(a^2)*(n^3)*(1-pr)*(meminus*veminus+m*proizvminus)...
+1.5*(a^2)*(n^3)*(pr*meplus+(1-pr)*meminus)*(pr*mbplus*m+(1-pr)*(veminus+mbminus*m));
udiff = o*((d.*(n^2)*m-(n^2)*mbplustwo.*m).^2)...
+odop*((i.*n-a*(n^2)*mbplustwo.*m-n*pr*meplus-n*(1-pr)*meminus+rp1).^2)...
+((a*(n^2)*mbplustwo.*m).^2)-o*((d.*(n^2)*m-(n^2)*veminus-(n^2)*mbminus*m).^2)...
-odop*((a*(n^2)*(veminus+mbminus*m)+n*pr*meplus-n*pr*meminus-rp1).^2)...
-((i.*n-n*meminus+a*(n^2)*(veminus+mbminus*m)).^2);
classcorrect = zeros(n_runs,1);
for run = 1:n_runs
classcorrect(run) = (1.5-classres(run))*udiff(run);
if classcorrect(run) >=0
count1(run) = 0;
count2(run) = 0;
else
count1(run) = abs(udiff(run));
count2(run) = 1;
end
end
if (sum(count2)==1)
count_ones = count_ones+1;
end
count_allruns = count_allruns+1;
count_allruns_store(outerrun) = count_allruns;
min_mistake = min(sum(count2), sum2_old);
if (sum(count2)<1) || (count_ones == 10) || ...
(count_allruns > 9999 && sum(count2)>sum2_old && min_mistake<30) || ...
(count_allruns == 11000)
break;
end
sum1_old2 = sum1_old;
sum1_old = sum(count1);
sum2_old = sum(count2);
class_start = classres;
count1_start = count1;
count2_start = count2;
end
if (sum(count2)>sum2_old)
classtotake = class_start;
else
classtotake = classres;
end
second_table = [second_table; table(i,d,classres, udiff, count1, count2, sum1ch, sum2ch, ...
class_start, count1_start, count2_start, mbplustwo, classtotake)];
store_eminus = mean(second_table.i(second_table.classtotake==1));
store_eplus = mean(second_table.i(second_table.classtotake==2));
store_bminus = mean(second_table.d(second_table.classtotake==1));
store_bplus = mean(second_table.d(second_table.classtotake==2));
store_pr = mean(second_table.classtotake)-1;
store_veminus = std(second_table.i(second_table.classtotake==1))*std(second_table.i(second_table.classtotake==1));
store_vbminus = std(second_table.d(second_table.classtotake==1))*std(second_table.d(second_table.classtotake==1));
store_vbplus = std(second_table.d(second_table.classtotake==2))*std(second_table.d(second_table.classtotake==2));
store_proizvminus = mean(second_table.d(second_table.classtotake==1))*mean(second_table.i(second_table.classtotake==1));
store_proizvplus = mean(second_table.d(second_table.classtotake==2))*mean(second_table.i(second_table.classtotake==2));
store_bplustwo_dop2 = zeros(10000, 1);
for rungraph7 = 1:10000
if (rungraph7>20)
min_bound = rungraph7-20;
else
min_bound = 1;
end
if (rungraph7<9980)
max_bound = rungraph7+21;
else
max_bound = 10000;
end
store_bplustwo_dop = mean(second_table.d(second_table.classtotake==2 & ...
second_table.i>xedges3(min_bound) & second_table.i<xedges3(max_bound)));
if isnan(store_bplustwo_dop)
store_bplustwo_dop = 0;
end
store_bplustwo_dop2(rungraph7) = store_bplustwo_dop;
end
store_bplustwo = smoothdata(store_bplustwo_dop2, 'gaussian', 1000);
store_rp1 = a*(n^2)*(store_pr*store_bplus*m...
+(1-store_pr)*(store_veminus+m*store_bminus))...
+a*(n^2)*store_pr*(1-store_pr)*((store_eplus-store_eminus)^2)...
+0.5*(a^3)*(n^4)*store_pr*(1-store_pr)*((store_bplus*m-store_veminus-store_bminus*m)^2)...
-1.5*(a^2)*(n^3)*store_pr*m*store_proizvplus...
-1.5*(a^2)*(n^3)*(1-store_pr)*(store_eminus*store_veminus+m*store_proizvminus)...
+1.5*(a^2)*(n^3)*(store_pr*store_eplus...
+(1-store_pr)*store_eminus)*(store_pr*store_bplus*m...
+(1-store_pr)*(store_veminus+store_bminus*m));
dgrid2 = linspace(0,2,10001);
igrid2 = icdf(p_i, linspace(0,1,10001));
[X2,Y2] = meshgrid(igrid2,dgrid2);
xedges2 = igrid2;
xedges2(1)=2*xedges2(2)-xedges2(3);
xedges2(10001)=2*xedges2(10000)-xedges2(9999);
yedges2 = dgrid2;
if (store_pr == 1)
store_eminus = 0;
store_bminus = 0;
store_veminus = 0;
store_vbminus = 0;
store_proizvminus = 0;
store_rp1 = a*(n^2)*(store_pr*store_bplus*m+(1-store_pr)*(store_veminus+store_bminus*m))...
+a*(n^2)*store_pr*(1-store_pr)*((store_eplus-store_eminus)^2)...
+0.5*(a^3)*(n^4)*store_pr*(1-store_pr)*((store_bplus*m-store_veminus-store_bminus*m)^2)...
-1.5*(a^2)*(n^3)*store_pr*m*store_proizvplus...
-1.5*(a^2)*(n^3)*(1-store_pr)*(store_eminus*store_veminus+m*store_proizvminus)...
+1.5*(a^2)*(n^3)*(store_pr*store_eplus+(1-store_pr)*store_eminus)*(store_pr*store_bplus*m...
+(1-store_pr)*(store_veminus+store_bminus*m));
end
if (store_pr == 0)
store_eplus = 0;
store_bplus = 0;
store_vbplus = 0;
store_proizvplus = 0;
store_rp1 = a*(n^2)*(store_pr*store_bplus*m+(1-store_pr)*(store_veminus+store_bminus*m))...
+a*(n^2)*store_pr*(1-store_pr)*((store_eplus-store_eminus)^2)...
+0.5*(a^3)*(n^4)*store_pr*(1-store_pr)*((store_bplus*m-store_veminus-store_bminus*m)^2)...
-1.5*(a^2)*(n^3)*store_pr*m*store_proizvplus...
-1.5*(a^2)*(n^3)*(1-store_pr)*(store_eminus*store_veminus+m*store_proizvminus)...
+1.5*(a^2)*(n^3)*(store_pr*store_eplus...
+(1-store_pr)*store_eminus)*(store_pr*store_bplus*m+(1-store_pr)*(store_veminus+store_bminus*m));
end
final_grid = zeros(length(igrid2)-1,length(dgrid2)-1);
final_grid2 = zeros(length(igrid2)-1,length(dgrid2)-1);
for rungraph3 = 1:size(X2,1)-1
for rungraph4 = 1:size(X2,2)-1
mid_i = (xedges2(rungraph3+1)+xedges2(rungraph3))/2;
mid_d = (yedges2(rungraph4+1)+yedges2(rungraph4))/2;
store_bplustwo_formula = store_bplustwo(rungraph3);
udiff_final = o*((mid_d*(n^2)*m-(n^2)*store_bplustwo_formula*m)^2)...
+odop*((mid_i*n-a*(n^2)*store_bplustwo_formula*m-n*store_pr*store_eplus-n*(1-store_pr)*store_eminus+store_rp1)^2)...
+((a*(n^2)*store_bplustwo_formula*m)^2)-o*((mid_d*(n^2)*m-(n^2)*store_veminus-(n^2)*store_bminus*m)^2)...
-odop*((a*(n^2)*(store_veminus+store_bminus*m)+n*store_pr*store_eplus-n*store_pr*store_eminus-store_rp1)^2)...
-((mid_i*n-n*store_eminus+a*(n^2)*(store_veminus+store_bminus*m))^2);
if udiff_final>0
udiff_finsign=0;
else
udiff_finsign=1;
end
final_grid(rungraph3,rungraph4) = udiff_finsign;
final_grid2(rungraph3,rungraph4) = udiff_final;
end
end
mean_final_grid2 = mean(final_grid2(:));
var_final_grid2 = var(final_grid2(:));
num_ones = sum(final_grid(:) == 1);
if (count_allruns_store(outerrun) > 9999)
infin_store=1;
else
infin_store=0;
end
proportion_ones = num_ones / numel(final_grid);
for i = 1:10
column_indices = i * 1000;
eval(['num_ones' num2str(i) ' = sum(final_grid(:, column_indices) == 1);']);
end
num_ones0 = sum(final_grid(:,1) == 1);
propminustwosigma = sum(final_grid(228,:) == 1) / size(final_grid,2);
propminussigma = sum(final_grid(1587,:) == 1) / size(final_grid,2);
propminushalfsigma = sum(final_grid(3086,:) == 1) / size(final_grid,2);
propminusquartersigma = sum(final_grid(4013,:) == 1) / size(final_grid,2);
propminuszerosigma = (sum(final_grid(5000,:) == 1)+sum(final_grid(5001,:) == 1))/(2*size(final_grid,2));
propplusquartersigma = sum(final_grid(5988,:) == 1) / size(final_grid,2);
propplushalfsigma = sum(final_grid(6915,:) == 1) / size(final_grid,2);
propplussigma = sum(final_grid(8414,:) == 1) / size(final_grid,2);
propplustwosigma = sum(final_grid(9773,:) == 1) / size(final_grid,2);
percentiles_eminus = 10000*normcdf(store_eminus, mui, sigmai);
percentiles_eplus = 10000*normcdf(store_eplus, mui, sigmai);
if (num_ones == 100000000)
zero_indices = NaN;
one_indices = find(final_grid == 1);
min_i = NaN;
max_i = NaN;
min_d = NaN;
max_d = NaN;
min_i1 = min(mod(one_indices - 1, size(final_grid, 1)) + 1);
max_i1 = max(mod(one_indices - 1, size(final_grid, 1)) + 1);
min_d1 = min(floor((one_indices - 1) / size(final_grid, 1)) + 1);
max_d1 = max(floor((one_indices - 1) / size(final_grid, 1)) + 1);
row_indices = NaN;
avg_row = NaN;
col_indices = NaN;
avg_col = NaN;
row_indices1 = floor((one_indices - 1) / size(final_grid, 1)) + 1;
avg_row1 = mean(row_indices1);
col_indices1 = mod(one_indices - 1, size(final_grid, 1)) + 1;
avg_col1 = mean(col_indices1);
variance_d_indices = NaN;
variance_d_indices1 = var(row_indices1);
variance_i_indices = NaN;
variance_i_indices1 = var(col_indices1);
table4 = [table4; table(proportion_ones, min_i, max_i, min_d, max_d, min_i1, max_i1, ...
min_d1, max_d1, variance_d_indices, variance_d_indices1, variance_i_indices, ...
variance_i_indices1, num_ones10, num_ones9, ...
num_ones8, num_ones7, num_ones6, num_ones5, num_ones4, ...
num_ones3, num_ones2, num_ones1, num_ones0, avg_row, avg_col, avg_row1, avg_col1, ...
mean_final_grid2, var_final_grid2, propminustwosigma, propminussigma, ...
propminushalfsigma, propminusquartersigma, propminuszerosigma, propplusquartersigma, ...
propplushalfsigma, propplussigma, propplustwosigma, a, e, f, g, h, j, k, l, m, n, o, ...
odop, mui, sigmai, store_eminus, store_eplus, store_bminus, store_bplus, store_pr, ...
store_veminus, store_vbminus, store_vbplus, store_proizvminus, store_proizvplus, ...
store_rp1, percentiles_eminus, percentiles_eplus, infin_store, min_mistake, count_allruns)];
elseif (num_ones == 0)
zero_indices = find(final_grid == 0);
one_indices = NaN;
min_i = min(mod(zero_indices - 1, size(final_grid, 1)) + 1);
max_i = max(mod(zero_indices - 1, size(final_grid, 1)) + 1);
min_d = min(floor((zero_indices - 1) / size(final_grid, 1)) + 1);
max_d = max(floor((zero_indices - 1) / size(final_grid, 1)) + 1);
min_i1 = NaN;
max_i1 = NaN;
min_d1 = NaN;
max_d1 = NaN;
row_indices = floor((zero_indices - 1) / size(final_grid, 1)) + 1;
avg_row = mean(row_indices);
col_indices = mod(zero_indices - 1, size(final_grid, 1)) + 1;
avg_col = mean(col_indices);
row_indices1 = NaN;
avg_row1 = NaN;
col_indices1 = NaN;
avg_col1 = NaN;
variance_d_indices = var(row_indices);
variance_d_indices1 = NaN;
variance_i_indices = var(col_indices);
variance_i_indices1 = NaN;
table4 = [table4; table(proportion_ones, min_i, max_i, min_d, max_d, min_i1, max_i1, ...
min_d1, max_d1, variance_d_indices, variance_d_indices1, variance_i_indices, ...
variance_i_indices1, num_ones10, num_ones9, num_ones8, ...
num_ones7, num_ones6, num_ones5, num_ones4, num_ones3, ...
num_ones2, num_ones1, num_ones0, avg_row, avg_col, avg_row1, avg_col1, ...
mean_final_grid2, var_final_grid2, propminustwosigma, propminussigma, ...
propminushalfsigma, propminusquartersigma, propminuszerosigma, ...
propplusquartersigma, propplushalfsigma, propplussigma, propplustwosigma, ...
a, e, f, g, h, j, k, l, m, n, o, odop, mui, sigmai, store_eminus, ...
store_eplus, store_bminus, store_bplus, store_pr, store_veminus, ...
store_vbminus, store_vbplus, store_proizvminus, store_proizvplus, ...
store_rp1, percentiles_eminus, percentiles_eplus, infin_store, min_mistake, count_allruns)];
else
zero_indices = find(final_grid == 0);
one_indices = find(final_grid == 1);
min_i = min(mod(zero_indices - 1, size(final_grid, 1)) + 1);
max_i = max(mod(zero_indices - 1, size(final_grid, 1)) + 1);
min_d = min(floor((zero_indices - 1) / size(final_grid, 1)) + 1);
max_d = max(floor((zero_indices - 1) / size(final_grid, 1)) + 1);
min_i1 = min(mod(one_indices - 1, size(final_grid, 1)) + 1);
max_i1 = max(mod(one_indices - 1, size(final_grid, 1)) + 1);
min_d1 = min(floor((one_indices - 1) / size(final_grid, 1)) + 1);
max_d1 = max(floor((one_indices - 1) / size(final_grid, 1)) + 1);
row_indices = floor((zero_indices - 1) / size(final_grid, 1)) + 1;
avg_row = mean(row_indices);
col_indices = mod(zero_indices - 1, size(final_grid, 1)) + 1;
avg_col = mean(col_indices);
row_indices1 = floor((one_indices - 1) / size(final_grid, 1)) + 1;
avg_row1 = mean(row_indices1);
col_indices1 = mod(one_indices - 1, size(final_grid, 1)) + 1;
avg_col1 = mean(col_indices1);
variance_d_indices = var(row_indices);
variance_d_indices1 = var(row_indices1);
variance_i_indices = var(col_indices);
variance_i_indices1 = var(col_indices1);
table4 = [table4; table(proportion_ones, min_i, max_i, min_d, max_d, min_i1, ...
max_i1, min_d1, max_d1, variance_d_indices, variance_d_indices1, ...
variance_i_indices, variance_i_indices1, num_ones10, num_ones9, num_ones8, ...
num_ones7, num_ones6, num_ones5, num_ones4, num_ones3, num_ones2, ...
num_ones1, num_ones0, avg_row, avg_col, avg_row1, avg_col1, ...
mean_final_grid2, var_final_grid2, propminustwosigma, propminussigma, ...
propminushalfsigma, propminusquartersigma, propminuszerosigma, ...
propplusquartersigma, propplushalfsigma, propplussigma, propplustwosigma, ...
a, e, f, g, h, j, k, l, m, n, o, odop, mui, sigmai, store_eminus, ...
store_eplus, store_bminus, store_bplus, store_pr, store_veminus, ...
store_vbminus, store_vbplus, store_proizvminus, store_proizvplus, ...
store_rp1, percentiles_eminus, percentiles_eplus, infin_store, min_mistake, count_allruns)];
end
end
writetable(table4, 'simulation_results.xlsx');