Simulating 100-Day Experiments with an Index Case

  • Context: MATLAB 
  • Thread starter Thread starter spoonyluv
  • Start date Start date
  • Tags Tags
    Experiments Index
Click For Summary
SUMMARY

The discussion focuses on simulating a 100-day experiment in MATLAB, where an index case is tracked over two iterations. The user initially struggles with resetting the index case value from 2 back to 1 for the second simulation. The solution provided involves placing the assignment of index=2 after the first loop or modifying the loop to iterate over the index variable directly. This ensures that the index case resets correctly for each simulation cycle.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with for loop constructs in programming
  • Knowledge of variable assignment and scope in MATLAB
  • Concept of simulating experiments in computational models
NEXT STEPS
  • Explore MATLAB for loop syntax and best practices
  • Learn about variable scope and lifecycle in MATLAB
  • Investigate techniques for simulating experiments in MATLAB
  • Study MATLAB's random number generation for simulating randomness
USEFUL FOR

Programmers, data scientists, and researchers interested in simulating experiments and modeling scenarios in MATLAB.

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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 29 ·
Replies
29
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K