Matlab: Plotting loops, arrays

In summary: Putting it just before the loop would work, but it's not the best solution. I think you should put it just after the loop, so it will run once for each loop.
  • #1
spoonyluv
9
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
  • #2
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!
 
  • #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
 
  • #4
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?
 
  • #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]. I am looking into resizing bob, but so far no luck..any ideas/suggestions would help!
 
  • #6
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:
  • #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;
 

What is a plotting loop in Matlab?

A plotting loop in Matlab is a programming technique used to plot multiple graphs or figures in a single figure window. It involves using a loop to execute a series of plotting commands, such as plot() or scatter(), to display data in a visually appealing manner.

How do you create an array in Matlab?

To create an array in Matlab, you can use the [ ] operator to enclose a set of elements, separated by commas or spaces. For example, myArray = [1, 2, 3, 4] creates an array with four elements. You can also use the linspace() function to generate equally spaced elements in an array.

What is the difference between a for loop and a while loop in Matlab?

A for loop in Matlab is used to execute a set of statements a specific number of times, while a while loop is used to execute a set of statements as long as a certain condition is true. In other words, a for loop is best used when the number of iterations is known beforehand, while a while loop is best used when the number of iterations is unknown.

How do you plot multiple arrays in a single figure in Matlab?

To plot multiple arrays in a single figure in Matlab, you can use the hold on command to prevent the previous plot from being cleared. Then, you can use the plot() or scatter() commands multiple times to plot different arrays on the same figure. Finally, use the legend() command to add a legend to the figure.

Can you change the type of graph plotted in a loop in Matlab?

Yes, you can change the type of graph plotted in a loop in Matlab. This can be done by using the plot() or scatter() commands with different parameters, such as changing the marker type or line style. You can also use the plotyy() function to plot two different types of graphs on the same figure.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
976
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
738
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top