Plotting a line between two points

조회 수: 11 (최근 30일)
Benedict Low
Benedict Low 2018년 11월 23일
댓글: Star Strider 2018년 11월 23일
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