Plotting a line between two points

13 views (last 30 days)
Hi,
I am encountering a problem trying to plot a line between two points (x1, y1; x2, y2). I have a dataset with alternating columns of x- and y-coordinate data (e.g. in a 10 by 10 matrix), and I added two new columns (e.g. columns 11 and 12) whereby column 11 is the mean x-coordinate of columns 1, 3, 5, ... and column 12 is the mean y-coordinate of columns 2, 4, 6,...
A(:,11)=mean(A(:,1:2:9),2);
A(:,12)=mean(A(:,2:2:10),2);
I want to plot a line between each point and the average so I use this as an example:
plot([A(1,1) A(1,11)],[A(1,2) A(1,12)])
This should plot a line between the first x- and y-coordinate and the mean x- and y-coordinate of the first row. However, the error message that I am getting is
"Data must be a single matrix Y or a list of pairs X,Y"
The frustrating thing is I tried it with randomly generated integers e.g.
A=randi(100,10,10)
and it works, but on my dataset it doesn't. I also noticed that the numeric format of the two additional columns I created are different, and I wonder if that plays a part.
Any help is appreciated.
Thank you.

Accepted Answer

Star Strider
Star Strider on 23 Nov 2018
You may need to transpose the (10x2) matrices for each column in order to plot them as you want.
Try this (for the first column of ‘A’):
figure
plot([A(:,1) A(:,11)]', [A(:,1) A(:,12)]')
  2 Comments
Benedict Low
Benedict Low on 23 Nov 2018
Thank you @Star Strider. I took out columns 11 and 12 and did a repmat to make the two matrices the same size. Then transposing the data as you advised worked.
Star Strider
Star Strider on 23 Nov 2018
As always, my pleasure.
The plot function works in mysterious ways.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!