How can I save my results of a loop in one table
1 次查看(过去 30 天)
显示 更早的评论
I have 25 parameter combination in my code (b&T --> 1:5).
In the third part of my code I am doing the anderson darling test on my combinations.
It tells me if ONE combination ( for example b=1 and T=1) is a good combination or bad. I want to see the results for all 25 combination. But with this code, I only see the last combination of the loop.
How can I save them all in a table?
clear all;
n = 10;
t0 = 0.5;
b = 1:5;
T = 1:5;
for v_T= 1:length(T)
for v_b= 1:length(b)
data(:,v_b,v_T) = wblrnd(v_b,v_T, [n,1]) + t0;
start = [1 0 0];
custompdf = @(x,a,b,c) (x>c).*(b/(a-c)).*(((x-c)/(a-c)).^(b-1)).*exp(-((x-c)/(a-c)).^b);
opt = statset('MaxIter',1e3,'MaxFunEvals',1e3,'FunValCheck','off');
params(v_b,1:3,v_T) = mle(data(:,v_b,v_T),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',[0 0 0],'UpperBound',[Inf Inf Inf])
end
end
for v_T= 1:length(T)
for v_b= 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
dist = makedist('weibull','a',T_A,'b',b_A)
[h,p] = adtest(data(:, v_b, v_T),'Distribution',dist)
end
end
采纳的回答
Star Strider
2020-9-16
The adtest function returns scalars for the outputs, so something like this could work:
[h(v_b, v_T),p(v_b, v_T)] = adtest(data(:, v_b, v_T),'Distribution',dist);
That will save the results in their respective matrices.
I cannot run your code, so I cannot test that.
14 个评论
Star Strider
2020-9-16
As always, my pleasure!
[vb,vT] = ndgrid(1:length(T),1:length(b));
T1 = table(vT(:), vb(:), h(:), p(:), 'VariableNames',{'v_T','v_b','h','p'});
.
Mustafa Vural
2020-9-16
It displays:
All table variables must have the same number of rows.
for v_T= 1:length(T)
for v_b= 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
dist = makedist('weibull','a',T_A,'b',b_A)
[h(v_b, v_T),p(v_b, v_T)] = adtest(data2p(:, v_b, v_T),'Distribution',dist)
[vb,vT] = ndgrid(1:length(T),1:length(b ));
T1 = table(vT(:), vb(:), h(:), p(:), 'VariableNames',{'v_T','v_b','h','p '});
end
end
Star Strider
2020-9-16
You need to put the ndgrid and table calls after the end of both loops. Then it works.
for v_T= 1:length(T)
for v_b= 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
dist = makedist('weibull','a',T_A,'b',b_A)
[h(v_b, v_T),p(v_b, v_T)] = adtest(data(:, v_b, v_T),'Distribution',dist)
end
end
[vb,vT] = ndgrid(1:length(T),1:length(b));
T1 = table(vT(:), vb(:), h(:), p(:), 'VariableNames',{'v_T','v_b','h','p'});
.
Mustafa Vural
2020-9-16
oh wooow it works so nice. Thats so cool! Thank you I appreciate that!
How can I do the same at the second part of my code? params are my estimated results.
First column are the estimated "T" values, second estimated "b" values and third estimated "t0" values.
It looks like this:
clear all;
n = 10;
t0 = 0.5;
b = 1:5;
T = 1:5;
for v_T= 1:length(T)
for v_b= 1:length(b)
data(:,v_b,v_T) = wblrnd(v_b,v_T, [n,1]) + t0;
start = [1 0 0];
custompdf = @(x,a,b,c) (x>c).*(b/(a-c)).*(((x-c)/(a-c)).^(b-1)).*exp(-((x-c)/(a-c)).^b);
opt = statset('MaxIter',1e3,'MaxFunEvals',1e3,'FunValCheck','off');
params(v_b,1:3,v_T) = mle(data(:,v_b,v_T),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',[0 0 0],'UpperBound',[Inf Inf Inf])
params3p(v_b,4,v_T) = T_A;
params3p(v_b,5,v_T) = b_A;
params3p(v_b,6,v_T) = t0;
params3p(v_b,7,v_T) = n;
end
end
Star Strider
2020-9-16
As always, my pleasure!
I am not able to run that.
The first problem is:
Unrecognized function or variable 'T_A'.
Error in ...
params3p(v_b,4,v_T) = T_A;
I suspect there may be others like it.
Mustafa Vural
2020-9-16
编辑:Mustafa Vural
2020-9-16
Ooh yes you right, I forgot something, now you can run it.
clear all;
n = 10;
t0 = 0.5;
b = 1:5;
T = 1:5;
data3p = zeros(n,length(b),length(T)); %Prelallocating
params3p = zeros(length(b),7,length(T)); %Prelallocating
for v_T= 1:length(T)
for v_b= 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
data3p(:,v_b,v_T) = wblrnd(v_b,v_T, [n,1]) + t0;
start = [1 0 0];
custompdf = @(x,a,b,c) (x>c).*(b/(a-c)).*(((x-c)/(a-c)).^(b-1)).*exp(-((x-c)/(a-c)).^b);
opt = statset('MaxIter',1e3,'MaxFunEvals',1e3,'FunValCheck','off');
params3p(v_b,1:3,v_T) = mle(data3p(:,v_b,v_T),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',[0 0 0],'UpperBound',[Inf Inf Inf])
params3p(v_b,4,v_T) = T_A;
params3p(v_b,5,v_T) = b_A;
params3p(v_b,6,v_T) = t0;
params3p(v_b,7,v_T) = n;
end
end
Star Strider
2020-9-17
That was a bit more involved because of the extra index. I created the second index as ‘iv’ since I have no idea how your code would otherwise refer to it. (You can obviously name it anything you want.)
Try this:
[vT,iv,vb] = meshgrid(1:length(T), (1:7), 1:length(b));
T2 = table(vT(:), iv(:), vb(:), params3p(:), 'VariableNames',{'v_T','iv','v_b','params3p'});
with:
First_9_Rows = T2(1:9,:)
producing:
First_9_Rows =
9×4 table
v_T iv v_b params3p
___ __ ___ ________
1 1 1 1.8079
1 2 1 1.7048
1 3 1 2.1389
1 4 1 2.6878
1 5 1 3.7165
1 6 1 0.49299
1 7 1 0.72493
2 1 1 0.7776
2 2 1 0.5925
Since ‘T2’ is a (175x4) tabble, and I do not understand what you are doing with this, I will let you check it to be certain it is correct. It might be necessasry to change the order of the meshgrid arguments, and the order of the associated outputs to match them, to represent the ‘params3p’ matrix correctly in ‘T2’.
Mustafa Vural
2020-9-17
I am sry I couldnt explain it correctly. I appreciate your help and your time. Its not so complicated I explain it to you. But you really helped me a lot.
In total, I have 25 combination of my given parameter (b, T, and t0),
because b=1:5; T=1:5; t0=0,5 --> (t0 is just one number, so its included in every combination).
- With wblrnd, I create random numbers and store it to data3p.
- With mle, I estimate the random numbers and receive 25 combination of estimated values.
- With anderson darling, I am testing if the random numbers in data3p are good results or bad.
- My first column are my 25 estimated T values.
- Second column: 25 estimated b values
- Third column: estimated t0 values
- Fourth column: given T values ( 1 1 1 1 1 2 2 2 2 2; and so on...)
- Fifth column: given b values ( 1 2 3 4 5 1 2 3 4 5; and so on...)
- Sixt column: given t0 values ( t0 t0 t0 t0 t0...)
- Seventh column: given n failure times ( n n n n n; ...)
If you run my below code, in "results3p", you can see how I store them. I thought I could change the result line, and do it like like you showed me in adtest, because it looks much more better. But you really helped me a lot, so I think can go on with this code.
clear all;
n = 10;
t0 = 0.5;
b = 1:5;
T = 1:5;
data3p = zeros(n,length(b),length(T)); %Preallocating
params3p = zeros(length(b),7,length(T)); %Preallocating
results3p = double.empty(length(b)*length(T), 0);
for v_T= 1:length(T)
for v_b= 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
data3p(:,v_b,v_T) = wblrnd(v_b,v_T, [n,1]) + t0;
start = [1 0 0];
custompdf = @(x,a,b,c) (x>c).*(b/(a-c)).*(((x-c)/(a-c)).^(b-1)).*exp(-((x-c)/(a-c)).^b);
opt = statset('MaxIter',1e3,'MaxFunEvals',1e3,'FunValCheck','off');
params3p(v_b,1:3,v_T) = mle(data3p(:,v_b,v_T),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',[0 0 0],'UpperBound',[Inf Inf Inf])
params3p(v_b,4,v_T) = T_A;
params3p(v_b,5,v_T) = b_A;
params3p(v_b,6,v_T) = t0;
params3p(v_b,7,v_T) = n;
end
results3p((v_T-1) * length(b) + 1:v_T*length(b), 1:size(params3p, 2)) = params3p(:,:,v_T);
end
Star Strider
2020-9-17
I am sorry, however at this point I am lost.
Appparently, the various parts of ‘param3p’ are separate columns of the table, however I have no idea how to assign them different variable names, if they even need different variable names. So the result should be a (25x7) table?
Also, the ‘results3p’ variable is new. I have no idea where it fits with the rest of this, or if it even does.
Mustafa Vural
2020-9-17
编辑:Mustafa Vural
2020-9-17
Yes, its a 25x7 in this case. If I change b to 1:3 and T to 1:3, its a 9x7 table.
params3p --> my estimatings for 3p weibull distribution.
params2p---> my estimatings for 2p weibull distribution.
This is my whole code:
clear all;
n = 100;
t0 = 0.5;
b = 1:5;
T = 1:5;
rng('shuffle');
result2p = double.empty(length(b)*length(T), 0); % A = ClassName.empty(sz1,...,szN) returns an empty array with the specified dimensions. At least one of the dimensions must be 0.
data2p = zeros(n,length(b),length(T)); % Preallocating: Geschwindigkeit des Durchlaufs erhöhen
params2p = zeros(length(b),7,length(T));
result3p = double.empty(length(b)*length(T), 0);
data3p = zeros(n,length(b),length(T));
params3p = zeros(length(b),7,length(T));
for v_T = 1:length(T)
for v_b = 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
data2p(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]);
params2p(v_b,1:2,v_T) = wblfit(data2p(:,v_b,v_T));
params2p(v_b,4,v_T) = T_A;
params2p(v_b,5,v_T) = b_A;
params2p(v_b,7,v_T) = n;
data3p(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]) + t0;
start = [1 0 0];
custompdf = @(x,a,b,c) (x>c).*(b/(a-c)).*(((x-c)/(a-c)).^(b-1)).*exp(-((x-c)/(a-c)).^b);
opt = statset('MaxIter',1e5,'MaxFunEvals',1e5,'FunValCheck','off');
params3p(v_b,1:3,v_T) = mle(data3p(:,v_b,v_T),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',[0 0 0],'UpperBound',[Inf Inf Inf])
params3p(v_b,4,v_T) = T_A;
params3p(v_b,5,v_T) = b_A;
params3p(v_b,6,v_T) = t0;
params3p(v_b,7,v_T) = n;
end
result3p((v_T-1)*length(b)+1:v_T*length(b), 1:size(params3p, 2)) = params3p(:,:,v_T);
result2p((v_T-1)*length(b)+1:v_T*length(b), 1:size(params2p, 2)) = params2p(:,:,v_T);
end
Star Strider
2020-9-17
I am not exactly certain how to create the table this time.
Try this:
[vT,vb] = meshgrid(1:length(T), 1:length(b));
T2p = array2table(result2p);
T3p = array2table(result3p);
T2 = [table(vT(:), vb(:), 'VariableNames',{'v_T','v_b'}), T2p, T3p];
creating 3 tables and concatenating them, with:
First9Rows = T2(1:9,:)
producing:
First9Rows =
9×16 table
v_T v_b result2p1 result2p2 result2p3 result2p4 result2p5 result2p6 result2p7 result3p1 result3p2 result3p3 result3p4 result3p5 result3p6 result3p7
___ ___ _________ _________ _________ _________ _________ _________ _________ _________ _________ _________ _________ _________ _________ _________
1 1 1.0287 1.0141 0 1 1 0 100 1.4767 0.96223 0.50666 1 1 0.5 100
1 2 1.0586 2.4242 0 1 2 0 100 1.5203 2.4382 0.43051 1 2 0.5 100
1 3 1.0107 3.6975 0 1 3 0 100 1.5031 2.6285 0.65671 1 3 0.5 100
1 4 0.96538 3.7811 0 1 4 0 100 1.5241 3.2105 0.73159 1 4 0.5 100
1 5 0.9754 5.0861 0 1 5 0 100 1.4811 5.481 0.37902 1 5 0.5 100
2 1 2.2783 1.0685 0 2 1 0 100 2.0837 0.8486 0.5487 2 1 0.5 100
2 2 2.0727 2.0381 0 2 2 0 100 2.6639 2.2925 0.54981 2 2 0.5 100
2 3 2.0826 3.1779 0 2 3 0 100 2.418 3.2345 0.38082 2 3 0.5 100
2 4 1.9133 3.394 0 2 4 0 100 2.3779 4.874 0.32657 2 4 0.5 100
I am using those matrices because ‘param2p’ and ‘param3p’ are both (5x7x5) matrices, and I cannot figure out how to create them as 2D matrices to create the table object, and be consistent with the ‘v_T’ and ‘v_b’ subscripts.
.
Mustafa Vural
2020-9-17
It looks really good, I really appreciate it. Thank you, you helped me a lot.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)