for loop adding numbers on left side of equal sign

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

 Accepted Answer

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 Comments

yes eventually I want it to work from k=1:1:200
however I tried to make the for loop run in a smaller array and than later expand it.
However when I run the code you suggested. The output is a large array of number for every pk(k) instead of just a single value per pk(k). Do you konw why this is?
pk(1) represent single value
pk(2) represent another single value
so on....
These are elements of pk array.
After run the code, type pk at the commant window, it return complete array elements of pk in the command window.
I see. However, What my goals was, was to create an array of
pk(1)=0
pk(2)=0
....
pk(100)=0.0039
pk(101)=0.0037
...
pk(200)
whereas at the moment, I only have the values that correspond to the p(k)'s without the p(k) itself
That represent the same thing
Check
pk(20)
%.............^..index Position
And
prob(20)
%...............^..index Position
Both are equal. May I getting your question?
@Kalyan: Preallocating pk is essential.
Yes @madhan ravi, is the following correct way?
pk=zeros(size(prob));
Thanks

Sign in to comment.

More Answers (1)

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 Comments

for k=89:1:105;
pk(k)=prob(k);
fprintf('\n when k=%d, value of p(%d)=%.5f',k,k,pk(k));
end
Output:
pd =
NormalDistribution
Normal distribution
mu = 100
sigma = 10
when k=89, value of p(89)=0.01942
when k=90, value of p(90)=0.02179
when k=91, value of p(91)=0.02420
when k=92, value of p(92)=0.02661
when k=93, value of p(93)=0.02897
when k=94, value of p(94)=0.03123
so on....
You can make the following also
when k=1, value of p(1)=0
when k=2, value of p(2)=0
when k=3, value of p(3)=0....
so on..
when k=89, value of p(89)=0.01942
when k=90, value of p(90)=0.02179
when k=91, value of p(91)=0.02420
when k=92, value of p(92)=0.02661
when k=93, value of p(93)=0.02897
when k=94, value of p(94)=0.03123
so on...
Please do some modification, you can do that also..Try!
Yes this is indeed what I was looking for.
Thank you a lot!
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!