Plotting a line between two points

12 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2018-11-23
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 个评论
Benedict Low
Benedict Low 2018-11-23
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 2018-11-23
As always, my pleasure.
The plot function works in mysterious ways.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by