MATLAB: How to create a loop for a matrix and plot the outputs in a graph

loopplot

I have a matrix that is called merged, the matrix is 100 x 2. I want to create a loop that breaks down the matrix into 5 small matrices (20 x 2) and I want to draw these matrices in one figure. I have done that using the below commands but I wonder if I can do that using a loop?
x = merged (1:100,1:2); a = x (1:20,:); b = x (21:40,:); c = x (41:60,:); d = x (61:80,:); e = x (81:100,:); hold on plot (a(:,1),a(:,2)) plot (b(:,1),b(:,2)) plot (c(:,1),c(:,2)) plot (d(:,1),d(:,2)) plot (e(:,1),e(:,2))

Best Answer

x = rand(100, 2);
axes('NextPlot', 'add'); % equivalent to "hold on"
for k = 1:5
a = (k - 1) * 20 + 1;
b = a + 19;
plot(x(a:b, 1), x(a:b, 2));
end
Or
for k = 1:20:100
plot(x(k:k+19, 1), x(k:k+19, 2));
end
Related Question