choose two coordinates

2 次查看(过去 30 天)
mohd
mohd 2012-4-19
hi there, i have a set of xy coordinates show in command window.So, I want to pick the coordinate where, there are the most many number repeated in y. here i attach the illustration for reference. see the attached for more information.
help me please.. thanks

采纳的回答

Matt Tearle
Matt Tearle 2012-4-19
idx = (y == mode(y));
x(idx)
y(idx)
or, if x and y are columns of a matrix
idx = (xy(:,2) == mode(xy(:,2)));
xy(idx,:)
EDIT TO ADD:
Note sure if I'm correctly interpreting your follow-up question, but try this (try a few times, because random numbers sometimes do strange things):
xy = randi(10,12,2); % Make some fake data
% Find the coordinates with the most equal y values
idx = (xy(:,2)==mode(xy(:,2)));
mostycoords = xy(idx,:);
% Take first and last coordinates
firstlast = mostycoords([1,end],:);
% Plot results
plot(xy(:,1),xy(:,2),'o',firstlast(:,1),firstlast(:,2))
axis([0,12,0,12])
  1 个评论
mohd
mohd 2012-4-19
thanks a lot sir..:-).you are very good response and understanding.:-). the code really i need. question again.. how to plot using that coordinates. i want to draw straight line using that coordinate on my image.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2012-4-19
Try this:
% Generate some sample data,
xy = [0 3; 1 2;3 4; 3 2; 2 5; 7 2; 3 9]
modeRowIndexes = xy(:,2) == mode(xy(:,2))
% Now we know the rows that have the mode in the second column.
% So now we can find the first and last row where they occur.
firstRow = find(modeRowIndexes, 1, 'first')
lastRow = find(modeRowIndexes, 1, 'last')
% Get the first occurrence (coordinate) into a 1 by 2 array.
firstCoordinate = [xy(firstRow, 1), xy(firstRow, 2)]
% Get the second occurrence (coordinate) into a 1 by 2 array.
lastCoordinate = [xy(lastRow, 1), xy(lastRow, 2)]
In the command window you'll see the explanation:
xy =
0 3
1 2
3 4
3 2
2 5
7 2
3 9
modeRowIndexes =
0
1
0
1
0
1
0
firstRow =
2
lastRow =
6
firstCoordinate =
1 2
lastCoordinate =
7 2
  1 个评论
mohd
mohd 2012-4-19
wow.. it really make me happy and has fun in matlab. thanks a lot sir..;-). i'm so worry if everyone can not understand what i mean. but it is easy to get understand my question. again thanks.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by