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;

 採用された回答

Voss
Voss 2021 年 12 月 11 日

0 投票

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 ExchangeGraph and Network Algorithms についてさらに検索

製品

リリース

R2021a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by