I have two populations of cells (main and sub). This model goes through a set of predefined mu values, finds the minmum AIC (least error) and spits out the number of Components. Ideally, we want to have two components, given there is two populations. It worked fine when I only iterated through mu of the main values. Now, I am tryig to also iterate through mu of the sub values. Instead of a 2D plot (mu_main vs numComponents), I want a 3D plot (mu_main, mu_sub, numComponents). However, it does not run. Takes too long, goes into an infinite loop maybe? Am I doing the nested for loop incorrectly?
This is the old code that workds perfectly:
n_main = 100000;
n_sub = 5000;
sigma_main = 0.1;
sigma_sub = 0.1;
mu_main = 1;
mu_sub = 1;
mumain_val = 1:0.02:3;
outputData = zeros(length(mumain_val), 2);
BestModel = cell(numel(mu_main),1);
allMus = cell(numel(mu_main),1);
for i = 1:length(mumain_val)
mumain_pos = i;
mu_main = mumain_val(mumain_pos);
y = sigma_main.*randn(n_main,1) + mu_main;
y2 = sigma_sub.*randn(n_sub,1) + mu_sub;
C = cat(1, y, y2);
AIC = zeros(1,4);
GMModels = cell(1,4);
options = statset('MaxIter',00);
mutemp = cell(4,1);
for k = 1:4
GMModels{k} = fitgmdist(C,k);
AIC(k)= GMModels{k}.AIC;
mutemp{k} = GMModels{k}.mu;
end
[minAIC,numComponents] = min(AIC);
outputData(i,1) = mu_main;
outputData(i,2) = numComponents;
allMus{i} = mutemp;
BestModel{i} = GMModels{outputData(i,2)};
end
figure(2)
scatter(outputData(:,1),outputData(:,2))
xlabel('mu main')
ylabel('numComponents')
This is the new code with the extra variable, nested for loop that is not running:
n_main = 100000;
n_sub = 5000;
sigma_main = 0.1;
sigma_sub = 0.1;
mu_main = 0.1;
mu_sub = 0.1;
mumain_val = 0.1:0.02:3;
musub_val = 0.1:0.02:3;
outputData = zeros(length(mumain_val), 3);
BestModel = cell(numel(mu_main),1);
allMus = cell(numel(mu_main),1);
for i = 1:length(mumain_val)
for j = 1:length(musub_val)
mumain_pos = i;
mu_main = mumain_val(mumain_pos);
musub_pos = i;
mu_sub = mumain_val(musub_pos);
y = sigma_main.*randn(n_main,1) + mu_main;
y2 = sigma_sub.*randn(n_sub,1) + mu_sub;
C = cat(1, y, y2);
AIC = zeros(1,4);
GMModels = cell(1,4);
options = statset('MaxIter',00);
mutemp = cell(4,1);
for k = 1:4
GMModels{k} = fitgmdist(C,k);
AIC(k)= GMModels{k}.AIC;
mutemp{k} = GMModels{k}.mu;
end
[minAIC,numComponents] = min(AIC);
outputData(i,1) = n_main;
outputData(i,2) = n_sub;
outputData(i,3) = numComponents;
BestModel{i} = GMModels{outputData(i,3)};
allMus{i} = BestModel{i}.mu;
end
end
figure(2)
scatter3(outputData(:,1),outputData(:,2),outputData(:,3))
xlabel('mu main')
ylabel('mu sub')
zlabel('numComponents')
PLEASE HELP ME