how do I create a table in a nested loop

조회 수: 12 (최근 30일)
hanife gürdal
hanife gürdal 2021년 4월 25일
댓글: Stephen23 2023년 9월 21일
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

답변 (1개)

Vishesh
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:
  1. Create a empty table and set its header name as "mu0","n","roz" and "rot".
  2. Add the updated values of "sonuc" to the table inside the "for" loop.
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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by