MATLAB Simulating 100-Day Experiments with an Index Case

  • Thread starter Thread starter spoonyluv
  • Start date Start date
  • Tags Tags
    Experiments Index
AI Thread Summary
The discussion revolves around running a simulation in MATLAB that involves a nested loop structure to simulate a process over 100 days, repeated twice. The user initially encounters a problem with resetting the value of an "index case" variable, which represents an infected individual. They want this variable to revert to its initial state at the start of each new simulation cycle (m=2). A suggested solution is to set the index variable to 1 before the loops begin and then change it to 2 after the first loop completes, ensuring that it resets correctly for the second iteration. An alternative approach proposed is to replace the outer loop variable with the index variable itself, allowing for a more straightforward management of the index state throughout the simulation. This ensures that the code behaves as intended without complications from variable scope or overwriting issues.
spoonyluv
Messages
9
Reaction score
0
Hello,

Here is my scenario: I am trying to run a code a 100 times, so I use a for loop statement i=1:100. The 1 to 100 stands for 1 to 100 days. Now I want to see how much randomness my results give and so I want to simulate this whole 100 days thing twice.

So I think my code would look something like this:

for m = 1:2
for i = 1:100

do something something

end
end

However here is the problem: within the code, do something something, i have this person that I have infected...I call him index case and initially assign him a value of 1 and within the period of 100 days I recover him and assign him a value of 2. so great. so the code works great from a period of 1-100. however when m=2 starts, I want the index case to go back to 1, and start the 100 day simulation again and I can't figure the best way to do that. The only thing I can think of is writing, towards the end, is when j > 100, index = 2, but I know that isn't right because Matlab will just ignore that part of the code since j never goes to greater than 100.

How do I get around this?

Thanks
 
Physics news on Phys.org
Just put index = 2 after the first loop
Code:
index=1;
for m = 1:2
     for i = 1:100

     do something something

     end
     index=2;
end
 
Or, instead of writing "for m=1:2", write "for index=1:2" and this should take care of everything
 
Back
Top