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

10 views (last 30 days)
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))

Accepted Answer

Jan
Jan on 18 Oct 2018
Edited: Jan on 18 Oct 2018
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

More Answers (0)

Categories

Find more on MATLAB 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!