필터 지우기
필터 지우기

I have troubles in for loop with 2 variables.

조회 수: 1 (최근 30일)
Chris
Chris 2022년 10월 7일
댓글: Rik 2022년 10월 9일
for n = 2:30
for x=1:n;
p(x) = (factorial(n)./(factorial(x).* factorial(n-x))).*((10/230).^x).*(1-(10/230).^(n-x));
end
end
I want to make like - when n=2: x=1,2. when n=3: x=1,2,3. , ..., when n=30: x=1,2,3, ....,30.
Therefore I think it should be 29 values but it has 30 values.
Would you tell me what do I have to fix?
  댓글 수: 2
Torsten
Torsten 2022년 10월 7일
편집: Torsten 2022년 10월 7일
I think you will have to include x = 0 to get the complete binomial distribution.
And (factorial(n)./(factorial(x).* factorial(n-x))) can be replaced by nchoosek(n,x).
Rik
Rik 2022년 10월 9일
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2022년 10월 7일
"Therefore I think it should be 29 values but it has 30 values."
But you said that when n = 30, x=1,2,3,...,30 -- which is 30 values.
Note that you are storing into p(x), where x is the inner loop control variable. For each new n value, it is going to overwrite each p(1:n-1) entries from the previous value of n, and then will write in a new value for p(n) in a location that has not been written into before. Overwriting locations is potentially the correct thing to do, but only in cases where you use the existing value -- for example, y(K) = Y(K) + exp(-t)/factorial(K);
Perhaps you want
for n = 2:30
for x=1:n;
p(n-1, x) = (factorial(n)./(factorial(x).* factorial(n-x))).*((10/230).^x).*(1-(10/230).^(n-x));
end
end
p
p = 29×30
0.0832 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1302 0.0054 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1739 0.0113 0.0003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2174 0.0189 0.0008 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2609 0.0284 0.0016 0.0001 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3043 0.0397 0.0029 0.0001 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3478 0.0529 0.0046 0.0003 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3913 0.0681 0.0069 0.0005 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.4348 0.0851 0.0099 0.0008 0.0000 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.4783 0.1040 0.0136 0.0012 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

추가 답변 (0개)

카테고리

Help CenterFile 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