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;

 Accepted Answer

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).

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!