Nested For Loop; Combine Two for loops

5 visualizaciones (últimos 30 días)
Elizabeth Cardona
Elizabeth Cardona el 21 de Mayo de 2020
Comentada: Elizabeth Cardona el 22 de Mayo de 2020
Hi, time is an important factor so I appreciate any help soon. Thank you!
I am writing code to identify two populations of cells with varying sigma, mu, and quanitities. So far, I am varying only the sigma_sub of the sub (smaller) population, while keeping the other variables constant. The way the code works is there is a for loop that iterates through a set of sigma_sub pre defined values, picks one postion of the iteration and sets sigma_sub to that value. Then, stores this value in an array through the length of the predefined values.
%% for loop for sigma sub values
% iterates through predefined values, picks position, assigns sigma value
sigmasub_val = 0.1:0.1:3;
a_sigmasub=[];
for i = 1:length(sigmasub_val)
sigmasub_pos = randi(length(sigmasub_val));
sigma_sub = sigmasub_val(sigmasub_pos);
a_sigmasub =[a_sigmasub;sigma_sub];
end
Next, this chaging value and the constant variables are used to find a model that best represents the data. The other for loop runs 4 tmes through different models to find the best one, and outputs the value of the numComponents of the best model for the given sigma_sub value and constants.
y = sigma_main.*randn(n_main,1) + mu_main; %10^6 SKBr3 cells
y2 = sigma_sub.*randn(n_sub,1) + mu_sub; %100,000 MDA MB 231
C = cat(1, y, y2);
AIC = zeros(1,4);
GMModels = cell(1,4);
options = statset('MaxIter',00);
for k = 1:4
GMModels{k} = fitgmdist(C,k);
AIC(k)= GMModels{k}.AIC;
end
[minAIC,numComponents] = min(AIC);
numComponents;
I need to find a way to combine this. So for every value of sigma_sub, have 4 models be tested on each value, and output the best model.
Does this make sense? Please help!

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 22 de Mayo de 2020
Elizabeth - perhaps you can combine the two as follows
sigmasub_val = 0.1:0.1:3;
outputData = zeros(length(sigmasub_val), 2); % <--- create an output array for sigmasub,numComponents
for i = 1:length(sigmasub_val)
sigmasub_pos = randi(length(sigmasub_val));
sigma_sub = sigmasub_val(sigmasub_pos);
y = sigma_main.*randn(n_main,1) + mu_main; %10^6 SKBr3 cells
y2 = sigma_sub.*randn(n_sub,1) + mu_sub; %100,000 MDA MB 231
C = cat(1, y, y2);
AIC = zeros(1,4);
GMModels = cell(1,4);
options = statset('MaxIter',00);
for k = 1:4
GMModels{k} = fitgmdist(C,k);
AIC(k)= GMModels{k}.AIC;
end
[minAIC,numComponents] = min(AIC);
outputData(i,1) = sigma_sub;
outputData(i,2) = numComponents;
end
Is that something close to what you are looking for?
  1 comentario
Elizabeth Cardona
Elizabeth Cardona el 22 de Mayo de 2020
This is it, exactly! Thank you. It just takes a very very long time to run.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Modeling en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by