Update a for loop issue.

3 次查看(过去 30 天)
Hi! I'm trying to create a counter to run through a for loop but update the for loop. There is a 0.1 probability of selecting the correct colored straw in a pool of 1000 straws and i want to figure out how many are lost each time time the trial is run for a total of 25 trials. So i am storing the outcome of running through each trial for the number of straws left. Any help would be appreciated to help me pin point what im missing!
so far I have
Initial_Number_Straws=1000
Total_Trials=1:1:25
Straws_Lost_Each_Trial=zeros(1,25)
Probability_Correct_Color=0.1
Straws_Lost_Each_Trial(1)=1000
for j=2:25
for i=1:Items_Left
probability=rand
if probability<=Probability_Correct_Color
Items_Left=Items_Left-1
end
Straws_Lost_Each_Trial(j)=Items_Left
end

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2020-5-11
编辑:KALYAN ACHARJYA 2020-5-11
Here are my submission:
  1. Please define the "Items_Left" before the for loop (may be original number of items, during start the execute), as it used in the for loop counter also.
  2. The Items_Left will be only updated (-1), when the if condition true, otherwise it will remain same.
It would be easy to answer, if you could ask the question with representative problem (Quite simpler to get it than as actual scenario)

更多回答(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