How Can I Store Loop Results in Arrays for Plotting in MATLAB?

Click For Summary

Discussion Overview

The discussion revolves around storing results from loops in MATLAB for the purpose of plotting. Participants are exploring methods to maintain and manipulate arrays that capture changes in state across iterations of nested loops, specifically focusing on how to structure and access these arrays effectively.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant shares a code snippet and seeks advice on how to store results from each iteration of a loop in an array for later plotting.
  • Another participant suggests using a 2D array or concatenating results into a single array to keep track of changes across iterations.
  • There is a query about where to place the array assignment within the loop structure, with advice given to initialize the array before the loop starts.
  • A participant reports success in implementing the suggested changes but encounters issues with the dimensions of the resulting array.
  • Another participant proposes using matrix transposition to manage array dimensions and suggests alternative methods for concatenating arrays.
  • A new participant raises a separate issue regarding storing matrices with dynamic names based on loop indices, asking for guidance on how to achieve this in MATLAB.

Areas of Agreement / Disagreement

Participants generally agree on the need to store results from loop iterations but present different methods and approaches for achieving this. There is no consensus on the best way to handle dynamic naming of matrices.

Contextual Notes

Some participants mention the need for proper initialization of arrays and the implications of MATLAB's handling of array dimensions, but specific limitations or unresolved steps are not detailed.

Who May Find This Useful

This discussion may be useful for MATLAB users looking to manage and visualize data generated from loops, particularly in the context of simulations or iterative calculations.

spoonyluv
Messages
9
Reaction score
0
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 want to 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...
 
Physics news on Phys.org
spoonyluv said:
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:
Code:
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 want to 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...

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!
 
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
 
spoonyluv said:
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

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:
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?
 
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]. I am looking into resizing bob, but so far no luck..any ideas/suggestions would help!
 
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:
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;
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K