Update a for loop issue.

3 views (last 30 days)
Amy Anderson
Amy Anderson on 11 May 2020
Edited: KALYAN ACHARJYA on 11 May 2020
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

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 11 May 2020
Edited: KALYAN ACHARJYA on 11 May 2020
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)

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!