How can I store the scalars in array after loop

m
I would like to save all the scalars from the loop (pcf, pc, ph) in 1-D arrays. Size of X is 100.
u1=18.877;
sigma1=0.411;
mu2=22.263;
sigma2=0.246;
mu3=22.961;
sigma3=0.635;
x=linspace(17,28);
pmd=normpdf(x,mu2,sigma2);
pld=normpdf(x,mu1,sigma1);
pud=normpdf(x,mu3,sigma3);
s=pmd+pld+pud;
for i=1:size(x)
pcf(i)=pmd(i)/s(i);
pc(i)=pld(i)/s(i);
ph(i)=pud(i)/s(i);
end
pcf;
pc;
ph;

 采纳的回答

Change this:
for i=1:size(x)
to this:
for i=1:numel(x)
because size(x) returns a vector of the sizes of x in each dimension, and using those sizes in a colon operation only uses the first one, which in this case is 1, so the loop was only executing one time (it was like for i=1:1). Using numel instead (length would also work in this case) will loop over all elements (like for i=1:100).

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2021a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by