Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab question

  1. Aug 5, 2009 #1
    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 cant 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 isnt 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
     
  2. jcsd
  3. Aug 6, 2009 #2
    Just put index = 2 after the first loop
    Code (Text):


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

         do something something

         end
         index=2;
    end
     
     
  4. Aug 6, 2009 #3
    Or, instead of writing "for m=1:2", write "for index=1:2" and this should take care of everything
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab question
  1. MATLAB questions (Replies: 0)

  2. MATLAB question (Replies: 2)

  3. MATLAB question (Replies: 1)

  4. Matlab question (Replies: 2)

  5. Matlab question (Replies: 10)

Loading...