how do I create a table in a nested loop

8 visualizaciones (últimos 30 días)
hanife gürdal
hanife gürdal el 25 de Abr. de 2021
Comentada: Stephen23 el 21 de Sept. de 2023
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

Respuestas (1)

Vishesh
Vishesh el 21 de Sept. de 2023
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":

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by