Hi,
for r=0:n;
y=factorial(n)/((factorial(n-r))*factorial(r))*((p)^(n-r))*(q)^(r);
end
How to store values of y in array?

 採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 8 月 18 日
編集済み: KALYAN ACHARJYA 2019 年 8 月 18 日

1 投票

i=1;
y=zeros(1,n+1);
for r=0:n;
y(i)=factorial(n)/((factorial(n-r))*factorial(r))*((p)^(n-r))*(q)^(r);
i=i+1;
end
Or if you can afford to change the for loop range
y=zeros(1,n+1);
for r=1:n+1 % Note: I have change the i srarting value
y(n)=factorial(n)/((factorial(n-r))*factorial(r))*((p)^(n-r))*(q)^(r);
end

5 件のコメント

Samadhan Mahajan
Samadhan Mahajan 2019 年 8 月 18 日
thank you sir
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 8 月 18 日
My pleasure Samadhan!
madhan ravi
madhan ravi 2019 年 8 月 18 日
Why do you need a loop? @Kalyan?
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 8 月 18 日
Yes, @Madhan, I didnot think about it, just answer the question how to store the array values, I did small modification on owner code only.
Yes agreed, Without loop is efficient.
Samadhan Mahajan
Samadhan Mahajan 2019 年 8 月 22 日
How can i store values of y in array without loop?
Because when i take the different values of y out of the loop it takes only final value. but i need all values of y.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by