for loop adding numbers on left side of equal sign

2 ビュー (過去 30 日間)
bus14
bus14 2019 年 5 月 3 日
コメント済み: KALYAN ACHARJYA 2019 年 5 月 3 日
Hi, I want to make a for loop which iterates the the values of my prob function. Ideally I want to the loop to do for [89-105] to give the sequence of pk(89)=prob(89) & pk(90)=prob(90) and so on. I already have the loop working that it gives the values of prob(k) in the correct sequence. However, I am unable to have the loop adding the values of i to be 89,90,91,92 and so. It must be a simple command but I cannot find it. Hope some can help me out.
pd= makedist('normal','mu',100,'sigma',10)
demand = [0:1:200];
prob = normpdf(demand,100,10);
for k=89:1:105;
pk(i)=prob(k)
i=k+1;
end
Furthermore, the error that I get running the code regarding the line pki)=prob(k) is [Subscript indices must either be real positive integers or logicals.]
Thankyou

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 3 日
編集済み: KALYAN ACHARJYA 2019 年 5 月 3 日
for k=89:1:105;
pk(k)=prob(k)
end
Yes, you can get it pk(89) to pk(105)
but what about pk(1), pk(2),...might be zero. yes?
  6 件のコメント
madhan ravi
madhan ravi 2019 年 5 月 3 日
@Kalyan: Preallocating pk is essential.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 3 日
編集済み: KALYAN ACHARJYA 2019 年 5 月 3 日
Yes @madhan ravi, is the following correct way?
pk=zeros(size(prob));
Thanks

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

その他の回答 (1 件)

bus14
bus14 2019 年 5 月 3 日
I think we misunderstand each other. What I mean is, that ideally I want to run the program and as an output obtain two 'colums'. first colum [pk(1) pk(2) pk(3) pk(4).......pk(200)]=[0 0 0 ....... 0.0035 0.00034]
easier said. I would like to see the as answer e.g. for the instance of k=100 get as output pk(100)=0.00397 instead of now only getting 0.000397
  3 件のコメント
bus14
bus14 2019 年 5 月 3 日
Yes this is indeed what I was looking for.
Thank you a lot!
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 3 日
pd=makedist('normal','mu',100,'sigma',10)
demand=[0:1:200];
prob=normpdf(demand,100,10);
prob(1:80)=0;
pk=zeros(size(prob));
for k=1:1:105;
pk(k)=prob(k);
fprintf('\n when k=%d, value of p(%d)=%.5f',k,k,pk(k));
end

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

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by