Trouble Using Hold for Plots

2 views (last 30 days)
Justin Kroes
Justin Kroes on 4 Oct 2015
Edited: dpb on 5 Oct 2015
I've written a script to output a 101x10 matrix. I'm trying to plot each column of the matrix (101x1) individually on the same graph. I can generate individual plots, but I can't trigger hold. Here's the code (I've omitted the custom function since the script itself successfully outputs the matrix):
X(1) = 0; %initial value of X at t=1
e = HopeThisWorks(100,0,1); %calls custom function 'HopeThisWorks with mean 0 and std 1
%Now we want to calculate a 101x10 matrix
for r = 1:10
for i = 1:100 %100 iterations
X(i+1,r) = .5 + X(i) + e(i);
end
end
t = 1:101
A = X(:,1)
B = X(:,2)
plot(t,A)
hold on
plot(t,B)
I've tried a couple ways of plotting, including 'for' loops.

Answers (1)

dpb
dpb on 4 Oct 2015
Edited: dpb on 5 Oct 2015
As written, there's no dependency on r (and should to preallocate X for full size to save overhead on dynamic allocation if retain the following loop solution altho that's not the problem with plotting).
Presumably, you meant to place the call to HopeThisWorks inside the loop on r to generate a new e vector for each column. Since it's outside and hence only called once, probably what is wrong with the generated plot is that the data for the two columns are identical so the two lines coincide but you've just not recognized that seeing only one line.
As for plotting; when you resolve those issues, then simply
plot(X)
will plot all columns against their index; you don't even need the t vector (altho it doesn't hurt to use it).

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!