How can I make a single plot that consists of multiple lines, each made from every n rows of a vector?

5 views (last 30 days)
I have looked everywhere and I can't seem to find anything for this. All I can find is how to plot every nth row, but I need to make a line for every n rows of a 2-column vector and overlay them all on the same graph. My data is very large so here is a simple example (assume col. 1 is x-data and col.2 is y-data):
A = [ 1 2
2 4
3 6
4 8
5 10
6 12 ]
I want a single plot that consists of two lines (in this case). The first being rows 1-3, where x1 = [1 2 3] and y1 = [2 4 6], and the second being rows 4-6, where x2 = [4 5 6] and y2 = [8 10 12].
I feel like this should be simple but I cannot seem to do it properly and the answers online are usually for every nth row, and not for what I need. Also, since I have large data, I'm sure I will need to use a loop because I won't be able to do this manually for a 300,000 x 2 set of data.

Accepted Answer

Stephen23
Stephen23 on 10 Jul 2019
Edited: Stephen23 on 10 Jul 2019
"I'm sure I will need to use a loop because I won't be able to do this manually for a 300,000 x 2 set of data."
I don't see why you would need to use a loop. Most likely using a loop would not be an effective use of MATLAB.
>> A = [1,2;2,4;3,6;4,8;5,10;6,12]
A =
1 2
2 4
3 6
4 8
5 10
6 12
>> n = 3;
>> X = reshape(A(:,1),n,[]);
>> Y = reshape(A(:,2),n,[]);
>> plot(X,Y,'*-')

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!