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

Matlab: Plotting loops, arrays

  1. Jul 1, 2009 #1
    Hello,

    I am trying to store the results in the form of an array every time the code goes through a loop. This is what my code looks like:

    for j = 1:100
    for i= 1:100
    if S(i)==0
    if rand < T
    S(i)= 1;
    t_inf(i) = j;
    end
    else
    if S(i)== 1
    if j-t_inf(i) > duration
    S(i) = 2;
    end
    end
    end
    end
    end

    So basically for the loop j =1:100, everytime the code goes through the j loop (for 100 times) and looks into the i loop and changes/does not change it, I would like to store the results of the change/non change for each loop. How do I do that? how can I store and view the results for each j loop? And the reason I wanna do this is that I want to plot the results of each loop to see how many 0, 1, 2's I have over time.....
     
  2. jcsd
  3. Jul 1, 2009 #2

    MATLABdude

    User Avatar
    Science Advisor

    You could do it with a 2D array using something along the lines of:
    results(i,j)=result_of_this_loop

    Or you could just concatenate it (without indices) onto the end of an array that you keep going:
    bob = [bob, result_of_this_loop]

    Edit: For future reference, Mr. Spoon, use the [CODE ] tag around your blocks of code, and your text formatting shows up nice and indented!
     
  4. Jul 1, 2009 #3
    Matlabdude,

    Where would I put the bob = [bob, result_of_this_loop] in my code below? after all the end statement? would I need to declare bob as something in the beginning of the code?

    Thanks
     
  5. Jul 2, 2009 #4

    MATLABdude

    User Avatar
    Science Advisor

    I'm not at a computer with MATLAB on it at the moment, but I don't think MATLAB likes you to do bob=[bob, actual_value] without bob having first been declared elsewhere (you could just try it and wait for MATLAB to throw up an error, or get back garbage). I'd initialize it with a null value just outside the loop:

    Code (Text):
    bob = [];
    % Loopy stuff  -- using the '%' sign declares the start of a comment to the end of the line, BTW
    %...STUFF...
    bob=[bob, current_loop_value];
    %...STUFF...
     
    Well, think about it for a second: if you put it just after an end statement terminating a loop, you'd never execute this statement until after the loop completed!

    So you have to put it inside the loop (probably at or near the end of the loop, but before the end statement) if you want it updated every iteration of the loop. Now you've got loops and conditionals going on within loops, so you need to figure out where you actually need this bit of code.

    ...You want it to update every iteration of the 'j' loop, right?
     
  6. Jul 2, 2009 #5
    Hey matlabdude,

    it worked! so thanks!

    however, now I have a new problem...i was hoping that as the loop continued, bob would retain the original dimensions of S, which is 2,5 (2 rows, 5 columns) . however bob is of the dimension [2, 500]. im looking into resizing bob, but so far no luck..any ideas/suggestions would help!
     
  7. Jul 3, 2009 #6

    MATLABdude

    User Avatar
    Science Advisor

    Maybe I'm not understanding correctly, but you've got something with 2 rows and 5 columns, and you want to produce something with x rows, and 5 columns, yes? (New rows are added per run of this loop?) If so (and even if not, you can play around with the basic concept) you can probably pull a trick using a matrix transpose operator (i.e. A' returns the transpose of A)

    bob = [bob', new_rows']'

    So, if bob is 4 rows by 5 columns, and your new_rows are 2 rows by 5 columns, bob' is 5 rows by 4 columns, and new_rows' is 5 rows by 2 columns.

    You concatenate them to form something that has 5 rows and 6 columns, and then you transpose again to get something that has 5 columns, and now 6 rows. You could also just right the stack at the end, after you've finished adding new columns.

    I suspect that bob=[bob; new_rows] might also work but there might be a caveat or two to that. Regardless, I'd try that before doing what I have above. You'll need to play around with either methodology to get it working 'just right'.
     
    Last edited: Jul 3, 2009
  8. Feb 7, 2010 #7
    dear sir/madam,
    i also facing this problem....i want to store the matrix X with different names as X1,X2,X3 for i=1 ,2,3 so that i can call later with X(i)... but its not working....can you t4ell me please how i can do this... kindly help me....

    m = 3;
    for (i = 1 :1:m)
    p = input( 'enter the species no:' ) ;

    n=10;
    x = [];
    for k=1:1:n
    a = input('enter the subgroup no');
    b = input('enter the quantity of that subgroup present ');
    if ( a==0) % Type subgroup no (a) = 0 to stop entry for this compound %
    break;
    end;
    y = [a b];
    x = [x ; y];

    end;
    x

    end;
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook