MATLAB Answers

Nested For Loop; Combine Two for loops

12 views (last 30 days)
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!

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 22 May 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 Comment

Elizabeth Cardona
Elizabeth Cardona on 22 May 2020
This is it, exactly! Thank you. It just takes a very very long time to run.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!