- Create a empty table and set its header name as "mu0","n","roz" and "rot".
- Add the updated values of "sonuc" to the table inside the "for" loop.
how do I create a table in a nested loop
조회 수: 10 (최근 30일)
이전 댓글 표시
there are nested for loops. I want to save them(sonuc) in the table.
I want to create a table with the 'sonuc' data, I want to see all the data in the same place, but it is repeated because it is in the loop.
Thank you very much in advance
clc
clear
mu0=10.5; sigma=1; alfa=0.05;den=1000;
n=10;nson=50;topz=0;topt=0
for mu0=10.5:0.5:11.5
mu0;
for n=10:10:nson
n;
for i=1:den
x1=normrnd (mu0,sigma,1,n);
x2=trnd(mu0,n-1);
[rz, pz, zh]= ztesti (x1,mu0,sigma,alfa);
[rt, pt, th]= ttesti (x2,mu0,alfa);
end
topz=topz+rz;
topt=topt+rt;
roz=topz/den;
rot=topt/den;
sonuc = [mu0, n, roz, rot]
end
end
댓글 수: 0
답변 (1개)
Vishesh
2023년 9월 21일
I understand that you want to create a table to save "sonuc" variable value that is getting updated within a "for" loop.
You can achieve this using following steps:
The updated script, after adding above steps, is provided below:
clc
clear
mu0=10.5;
sigma=1;
alfa=0.05;
den=1000;
n=10;
nson=50;
topz=0;
topt=0;
header={'mu0','n','roz','rot'}; % header of the table
sonuc=cell2table(cell(0,4),'VariableNames',header); % empty table
for mu0=10.5:0.5:11.5
mu0;
for n=10:10:nson
n;
for i=1:den
x1=normrnd (mu0,sigma,1,n);
x2=trnd(mu0,n-1);
[rz, pz, zh]= ztesti (x1,mu0,sigma,alfa);
[rt, pt, th]= ttesti (x2,mu0,alfa);
end
topz=topz+rz;
topt=topt+rt;
roz=topz/den;
rot=topt/den;
sonuc = [sonuc;table(mu0, n, roz, rot,'VariableNames',header)]; % adding data to the table.
end
end
Please refer to the following documentation for more information on "table":
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!