# I have troubles in for loop with 2 variables.

1 view (last 30 days)

Show older comments

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 Comments

Rik
on 9 Oct 2022

### Accepted Answer

Walter Roberson
on 7 Oct 2022

"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

##### 0 Comments

### More Answers (0)

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!