# how to extract variables from cell (dot operators)

조회 수: 7(최근 30일)
Elizabeth Cardona 2020년 5월 31일
I have a 1x19 cell with gmdistributions (BestModel). Each one of these cells gm distribution has mu values that I need, and I want to store in some sort of array so that I can plot these vs iterating sigma_main value. How can I do this? The variables get stored in the cell by using "fitgmdist" and are accessible via dot operator.
For every sigma_main value, I want the mu values to be extracted and then plotted vs sigma_main. There might be 1,2,3, or 4 mu values for every sigma_main depending on the numComponents.
Does this make sense? Here is my code:
%% define known constants
% rng(0,'twister')
n_main = 100000;
n_sub = 10000;
sigma_main = 0.1;
sigma_sub = 0.1;
mu_main = 1;
mu_sub =3;
%% change sigma_main values and run model
sigmamain_val = 0.1:0.05:1;
outputData = zeros(length(sigmamain_val), 2); % create an output array for (length sigma_sub),numComponents (19x2 double)
BestModel = cell(numel(sigma_main),1); % pre-allocate array to hold fitgmdist outputs (1x19 cell)
for i = 1:length(sigmamain_val)
%sigmamain_pos = randi(length(sigmamain_val));
sigmamain_pos = i; %iteration
sigma_main = sigmamain_val(sigmamain_pos); % sigma main from every iteration
y = sigma_main.*randn(n_main,1) + mu_main; %100000 main cells
y2 = sigma_sub.*randn(n_sub,1) + mu_sub; %10000 sub MB 231
C = cat(1, y, y2);
AIC = zeros(1,4); % create an ouput array for AIC
GMModels = cell(1,4); %pre allocate (1x4 cell)
options = statset('MaxIter',00); % add optitons here to better/refine the model
%mu = zeros(2,1);
for k = 1:4
GMModels{k} = fitgmdist(C,k);
AIC(k)= GMModels{k}.AIC;
%mu(k) = GMModels{k}.mu
end
[minAIC,numComponents] = min(AIC);
outputData(i,1) = sigma_main;
outputData(i,2) = numComponents;
%BestModel = GMModels{numComponents} %gives only one sigma main
BestModel{i} = GMModels{outputData(i,2)}; %(1x19 cell with gm distributions)
end
%% plot sigma main vs numComponents
figure(3)
scatter(outputData(:,1),outputData(:,2))
xlabel('sigma main')
ylabel('numComponents')
I tried:
mu(k) = GMModels{k}.mu
I also tried
mu{k} = GMModels{k}.mu
But I get the error that the "left and right side are not equivalent".
Please tell me how to do this (pre allocate, inside for loop, and after) and how to plot this if you can

댓글을 달려면 로그인하십시오.

### 채택된 답변

Thiago Henrique Gomes Lobato 2020년 5월 31일
편집: Thiago Henrique Gomes Lobato 2020년 5월 31일
You don't need to pre allocate since the best models are already in your cell array. I believe what you want may be something like this:
%% Plot mu with sigmain
figure,
for idx=1:length(BestModel)
mu = BestModel{idx}.mu; % get all mu's
plot(ones(size(mu,1),1)*sigmamain_val(idx),mu,'bo') % plot all in the same sigma value as scatter plot
hold on
end
xlabel('Sigma')
ylabel('mu')
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Thiago Henrique Gomes Lobato 2020년 6월 1일
yes, one way could be:
...
BestModel = cell(numel(sigma_main),1); % pre-allocate array to hold fitgmdist outputs (1x19 cell)
Allmus = cell(numel(sigma_main),1);
for i = 1:length(sigmamain_val)
...
mutemp = cell(4,1);
for k = 1:4
GMModels{k} = fitgmdist(C,k);
AIC(k)= GMModels{k}.AIC;
mutemp{k} = GMModels{k}.mu;
end
Allmus{i} = mutemp;
...

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by